123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using UnityEngine;
- using System.Collections;
- public class PlayerMovement : MonoBehaviour
- {
- public float turnSmoothing = 15f; // A smoothing value for turning the player.
- public float speedDampTime = 0.1f; // The damping for the speed parameter
- private Animator anim; // Reference to the animator component.
- private HashIDs hash; // Reference to the HashIDs.
-
-
- void Awake ()
- {
- // Setting up the references.
- anim = GetComponent<Animator>();
- hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
- }
-
-
- void FixedUpdate ()
- {
- // Cache the inputs.
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- //MovementManagement(h, v, sneak);
- MovementManagement(h, v);
- }
-
-
- void Update ()
- {
- AudioManagement();
- }
-
-
- void MovementManagement (float horizontal, float vertical)
- {
- // 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(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
- }
- else
- // Otherwise set the speed parameter to 0.
- anim.SetFloat(hash.speedFloat, 0);
- }
-
-
- 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(GetComponent<Rigidbody>().rotation, targetRotation, turnSmoothing * Time.deltaTime);
-
- // Change the players rotation to this new rotation.
- GetComponent<Rigidbody>().MoveRotation(newRotation);
- }
-
-
- void AudioManagement ()
- {
- // If the player is currently in the run state...
- if(anim.GetCurrentAnimatorStateInfo(0).fullPathHash == hash.locomotionState)
- //if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
- {
- // ... and if the footsteps are not playing...
- if(!GetComponent<AudioSource>().isPlaying)
- // ... play them.
- GetComponent<AudioSource>().Play();
- }
- else
- // Otherwise stop the footsteps.
- GetComponent<AudioSource>().Stop();
- }
- }
|