Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class MovementSystemScript : MonoBehaviour {
- public float turnSmoothing = 15f; // A smoothing value for turning the player.
- public float speedDampTime = 0.1f; // The damping for the speed parameter
- public int speedFloat;
- public int sneakingBool;
- public int jumpingBool;
- public int groundedBool;
- private Animator anim; // Reference to the animator component.
- private AnimatorStateInfo currentBaseState;
- public float JumpForce = 300.0f;
- public bool grounded = true;
- public float BaseSpeed = 0.1f;
- private float rayLength;
- private Transform thisTransform;
- private Vector3 movement = Vector3.zero;
- Vector3 raycastOffset;
- new private CapsuleCollider collider;
- public float Extra = 0.2f;
- public bool doJump = false;
- private Vector3 groundNormal;
- public float AccelRate;
- public float DecelRate;
- public float MoveSpeed;
- public float length;
- void Awake ()
- {
- this.collider = (CapsuleCollider)base.collider;
- // Setting up the references.
- anim = GetComponent<Animator>();
- speedFloat = Animator.StringToHash("Speed");
- sneakingBool = Animator.StringToHash("Sneaking");
- jumpingBool = Animator.StringToHash("Jump");
- groundedBool = Animator.StringToHash("Grounded");
- raycastOffset = new Vector3(collider.center.x, collider.center.y - (collider.height * 0.5f) + collider.radius + 0.05f, collider.center.z);
- PhysicMaterial controllerMat = new PhysicMaterial();
- controllerMat.bounciness = 0.0f;
- controllerMat.dynamicFriction = 0.0f;
- controllerMat.staticFriction = 0.0f;
- controllerMat.bounceCombine = PhysicMaterialCombine.Minimum;
- controllerMat.frictionCombine = PhysicMaterialCombine.Minimum;
- collider.material = controllerMat;
- }
- // Use this for initialization
- void Start () {
- rayLength = collider.bounds.extents.y + 0.1f;
- thisTransform = GetComponent<Transform>();
- }
- // Update is called once per frame
- void Update () {
- if (Input.GetButtonDown("Jump")) {
- doJump = true;
- }
- }
- void FixedUpdate ()
- {
- // Cache the inputs.
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- bool sneak = Input.GetButton("Sneak");
- bool jump = Input.GetButton("Jump");
- if (doJump)
- {
- doJump = false;
- if (grounded)
- {
- grounded = false;
- anim.SetBool ("Grounded", false);
- rigidbody.AddForce(0, JumpForce, 0);
- }
- }
- RaycastHit hit;
- if (Physics.SphereCast (transform.position + raycastOffset, collider.radius * 0.9f, Vector3.down, out hit, Extra + 0.05f) && GroundAngle (hit.normal) <= 45.0f) {
- grounded = true;
- anim.SetBool ("Grounded", true);
- groundNormal = hit.normal;
- } else {
- grounded = false;
- anim.SetBool ("Grounded", false);
- }
- MovementManagement(h, v, sneak, jump);
- }
- private float GroundAngle(Vector3 normal)
- {
- return (float)System.Math.Acos((double)normal.y) * Mathf.Rad2Deg;
- }
- void MovementManagement (float horizontal, float vertical, bool sneaking, bool jumping)
- {
- // Set the sneaking parameter to the sneak input.
- anim.SetBool(sneakingBool, sneaking);
- anim.SetBool(jumpingBool, jumping);
- // If there is some axis input...
- if(horizontal != 0f || vertical != 0f)
- {
- // ... set the players rotation and set the speed parameter to 5.5f.
- Rotating(horizontal, vertical);
- anim.SetFloat(speedFloat, 5.5f, speedDampTime, Time.deltaTime);
- }
- else
- // Otherwise set the speed parameter to 0.
- anim.SetFloat(speedFloat, 0);
- // align our movement vectors with the ground normal (ground normal = up)
- Vector3 targetSpeed = Vector3.Cross(groundNormal, transform.forward) * (Input.GetAxis("Horizontal") * MoveSpeed) + Vector3.Cross(transform.right, groundNormal) * (Input.GetAxis("Vertical") * MoveSpeed);
- length = targetSpeed.magnitude;
- float difference = length - rigidbody.velocity.magnitude;
- // avoid divide by zero
- if (Mathf.Approximately(difference, 0.0f))
- movement = Vector3.zero;
- else
- {
- float acceleration;
- // determine if we should accelerate or decelerate
- if (difference > 0.0f)
- acceleration = Mathf.Min(AccelRate * Time.deltaTime, difference);
- else
- acceleration = Mathf.Max(-DecelRate * Time.deltaTime, difference);
- difference = 1.0f / difference * acceleration;
- movement = new Vector3((targetSpeed.x - rigidbody.velocity.x) * difference, (targetSpeed.y - rigidbody.velocity.y) * difference, (targetSpeed.z - rigidbody.velocity.z) * difference);
- }
- rigidbody.AddForce(movement, ForceMode.VelocityChange);
- }
- void Rotating (float horizontal, float vertical)
- {
- // Create a new vector of the horizontal and vertical inputs.
- Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
- // Create a rotation based on this new vector assuming that up is the global y axis.
- Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
- // Create a rotation that is an increment closer to the target rotation from the player's rotation.
- Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
- // Change the players rotation to this new rotation.
- rigidbody.MoveRotation(newRotation);
- rigidbody.transform.Translate (Vector3.forward * BaseSpeed);
- }
- void OnCollisionStay(Collision collisionInfo)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement