Advertisement
Guest User

Untitled

a guest
Jan 5th, 2015
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.36 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MovementSystemScript : MonoBehaviour {
  5.    
  6.     public float turnSmoothing = 15f;   // A smoothing value for turning the player.
  7.     public float speedDampTime = 0.1f;  // The damping for the speed parameter
  8.     public int speedFloat;
  9.     public int sneakingBool;
  10.     public int jumpingBool;
  11.     public int groundedBool;
  12.     private Animator anim;              // Reference to the animator component.
  13.     private AnimatorStateInfo currentBaseState;
  14.     public float JumpForce = 300.0f;
  15.     public bool grounded = true;
  16.     public float BaseSpeed = 0.1f;
  17.     private float rayLength;
  18.     private Transform thisTransform;
  19.     private Vector3 movement = Vector3.zero;
  20.     Vector3 raycastOffset;
  21.     new private CapsuleCollider collider;
  22.     public float Extra = 0.2f;
  23.     public bool doJump = false;
  24.  
  25.     private Vector3 groundNormal;
  26.     public float AccelRate;
  27.     public float DecelRate;
  28.     public float MoveSpeed;
  29.     public float length;
  30.      
  31.     void Awake ()
  32.     {  
  33.         this.collider = (CapsuleCollider)base.collider;
  34.         // Setting up the references.
  35.         anim = GetComponent<Animator>();
  36.         speedFloat = Animator.StringToHash("Speed");
  37.         sneakingBool = Animator.StringToHash("Sneaking");
  38.         jumpingBool = Animator.StringToHash("Jump");
  39.         groundedBool = Animator.StringToHash("Grounded");
  40.         raycastOffset = new Vector3(collider.center.x, collider.center.y - (collider.height * 0.5f) + collider.radius + 0.05f, collider.center.z);
  41.    
  42.         PhysicMaterial controllerMat = new PhysicMaterial();
  43.         controllerMat.bounciness = 0.0f;
  44.         controllerMat.dynamicFriction = 0.0f;
  45.         controllerMat.staticFriction = 0.0f;
  46.         controllerMat.bounceCombine = PhysicMaterialCombine.Minimum;
  47.         controllerMat.frictionCombine = PhysicMaterialCombine.Minimum;
  48.         collider.material = controllerMat;
  49.     }
  50.  
  51.     // Use this for initialization
  52.     void Start () {
  53.         rayLength = collider.bounds.extents.y + 0.1f;
  54.         thisTransform = GetComponent<Transform>();
  55.     }
  56.    
  57.    
  58.     // Update is called once per frame
  59.     void Update () {
  60.         if (Input.GetButtonDown("Jump")) {
  61.             doJump = true;
  62.         }      
  63.     }
  64.    
  65.     void FixedUpdate ()
  66.     {
  67.         // Cache the inputs.
  68.         float h = Input.GetAxis("Horizontal");
  69.         float v = Input.GetAxis("Vertical");
  70.         bool sneak = Input.GetButton("Sneak");
  71.         bool jump = Input.GetButton("Jump");
  72.  
  73.         if (doJump)
  74.         {
  75.             doJump = false;
  76.            
  77.             if (grounded)
  78.             {
  79.                 grounded = false;
  80.                 anim.SetBool ("Grounded", false);
  81.                 rigidbody.AddForce(0, JumpForce, 0);
  82.             }
  83.         }
  84.  
  85.         RaycastHit hit;
  86.         if (Physics.SphereCast (transform.position + raycastOffset, collider.radius * 0.9f, Vector3.down, out hit, Extra + 0.05f) && GroundAngle (hit.normal) <= 45.0f) {
  87.             grounded = true;
  88.             anim.SetBool ("Grounded", true);
  89.             groundNormal = hit.normal;
  90.  
  91.         } else {
  92.             grounded = false;
  93.             anim.SetBool ("Grounded", false);
  94.         }
  95.  
  96.         MovementManagement(h, v, sneak, jump);
  97.     }
  98.  
  99.  
  100.     private float GroundAngle(Vector3 normal)
  101.     {
  102.         return (float)System.Math.Acos((double)normal.y) * Mathf.Rad2Deg;
  103.     }
  104.  
  105.     void MovementManagement (float horizontal, float vertical, bool sneaking, bool jumping)
  106.     {
  107.         // Set the sneaking parameter to the sneak input.
  108.         anim.SetBool(sneakingBool, sneaking);
  109.        
  110.         anim.SetBool(jumpingBool, jumping);
  111.         // If there is some axis input...
  112.         if(horizontal != 0f || vertical != 0f)
  113.         {
  114.             // ... set the players rotation and set the speed parameter to 5.5f.
  115.             Rotating(horizontal, vertical);
  116.             anim.SetFloat(speedFloat, 5.5f, speedDampTime, Time.deltaTime);
  117.         }
  118.         else
  119.             // Otherwise set the speed parameter to 0.
  120.             anim.SetFloat(speedFloat, 0);
  121.  
  122.         // align our movement vectors with the ground normal (ground normal = up)
  123.         Vector3 targetSpeed = Vector3.Cross(groundNormal, transform.forward) * (Input.GetAxis("Horizontal") * MoveSpeed) + Vector3.Cross(transform.right, groundNormal) * (Input.GetAxis("Vertical") * MoveSpeed);
  124.        
  125.         length = targetSpeed.magnitude;
  126.         float difference = length - rigidbody.velocity.magnitude;
  127.        
  128.         // avoid divide by zero
  129.         if (Mathf.Approximately(difference, 0.0f))
  130.             movement = Vector3.zero;
  131.        
  132.         else
  133.         {
  134.             float acceleration;
  135.            
  136.             // determine if we should accelerate or decelerate
  137.             if (difference > 0.0f)
  138.                 acceleration = Mathf.Min(AccelRate * Time.deltaTime, difference);
  139.             else
  140.                 acceleration = Mathf.Max(-DecelRate * Time.deltaTime, difference);
  141.            
  142.             difference = 1.0f / difference * acceleration;
  143.             movement = new Vector3((targetSpeed.x - rigidbody.velocity.x) * difference, (targetSpeed.y - rigidbody.velocity.y) * difference, (targetSpeed.z - rigidbody.velocity.z) * difference);
  144.         }
  145.        
  146.         rigidbody.AddForce(movement, ForceMode.VelocityChange);
  147.     }
  148.    
  149.     void Rotating (float horizontal, float vertical)
  150.     {
  151.         // Create a new vector of the horizontal and vertical inputs.
  152.         Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
  153.        
  154.         // Create a rotation based on this new vector assuming that up is the global y axis.
  155.         Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
  156.        
  157.         // Create a rotation that is an increment closer to the target rotation from the player's rotation.
  158.         Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
  159.        
  160.         // Change the players rotation to this new rotation.
  161.         rigidbody.MoveRotation(newRotation);
  162.         rigidbody.transform.Translate (Vector3.forward * BaseSpeed);
  163.     }
  164.    
  165.     void OnCollisionStay(Collision collisionInfo)
  166.     {
  167.  
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement