FirstPersonController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using UnityEngine;
  3. using UnityStandardAssets.CrossPlatformInput;
  4. using UnityStandardAssets.Utility;
  5. using Random = UnityEngine.Random;
  6. #pragma warning disable 618, 649
  7. namespace UnityStandardAssets.Characters.FirstPerson
  8. {
  9. [RequireComponent(typeof (CharacterController))]
  10. [RequireComponent(typeof (AudioSource))]
  11. public class FirstPersonController : MonoBehaviour
  12. {
  13. [SerializeField] private bool m_IsWalking;
  14. [SerializeField] private float m_WalkSpeed;
  15. [SerializeField] private float m_RunSpeed;
  16. [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
  17. [SerializeField] private float m_JumpSpeed;
  18. [SerializeField] private float m_StickToGroundForce;
  19. [SerializeField] private float m_GravityMultiplier;
  20. [SerializeField] private MouseLook m_MouseLook;
  21. [SerializeField] private bool m_UseFovKick;
  22. [SerializeField] private FOVKick m_FovKick = new FOVKick();
  23. [SerializeField] private bool m_UseHeadBob;
  24. [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
  25. [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
  26. [SerializeField] private float m_StepInterval;
  27. [SerializeField] private AudioClip[] m_FootstepSounds; // an array of footstep sounds that will be randomly selected from.
  28. [SerializeField] private AudioClip m_JumpSound; // the sound played when character leaves the ground.
  29. [SerializeField] private AudioClip m_LandSound; // the sound played when character touches back on ground.
  30. private Camera m_Camera;
  31. private bool m_Jump;
  32. private float m_YRotation;
  33. private Vector2 m_Input;
  34. private Vector3 m_MoveDir = Vector3.zero;
  35. private CharacterController m_CharacterController;
  36. private CollisionFlags m_CollisionFlags;
  37. private bool m_PreviouslyGrounded;
  38. private Vector3 m_OriginalCameraPosition;
  39. private float m_StepCycle;
  40. private float m_NextStep;
  41. private bool m_Jumping;
  42. private AudioSource m_AudioSource;
  43. // Use this for initialization
  44. private void Start()
  45. {
  46. m_CharacterController = GetComponent<CharacterController>();
  47. m_Camera = Camera.main;
  48. m_OriginalCameraPosition = m_Camera.transform.localPosition;
  49. m_FovKick.Setup(m_Camera);
  50. m_HeadBob.Setup(m_Camera, m_StepInterval);
  51. m_StepCycle = 0f;
  52. m_NextStep = m_StepCycle/2f;
  53. m_Jumping = false;
  54. m_AudioSource = GetComponent<AudioSource>();
  55. m_MouseLook.Init(transform , m_Camera.transform);
  56. }
  57. // Update is called once per frame
  58. private void Update()
  59. {
  60. RotateView();
  61. // the jump state needs to read here to make sure it is not missed
  62. if (!m_Jump)
  63. {
  64. m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
  65. }
  66. if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
  67. {
  68. StartCoroutine(m_JumpBob.DoBobCycle());
  69. PlayLandingSound();
  70. m_MoveDir.y = 0f;
  71. m_Jumping = false;
  72. }
  73. if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
  74. {
  75. m_MoveDir.y = 0f;
  76. }
  77. m_PreviouslyGrounded = m_CharacterController.isGrounded;
  78. }
  79. private void PlayLandingSound()
  80. {
  81. m_AudioSource.clip = m_LandSound;
  82. m_AudioSource.Play();
  83. m_NextStep = m_StepCycle + .5f;
  84. }
  85. private void FixedUpdate()
  86. {
  87. float speed;
  88. GetInput(out speed);
  89. // always move along the camera forward as it is the direction that it being aimed at
  90. Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
  91. // get a normal for the surface that is being touched to move along it
  92. RaycastHit hitInfo;
  93. Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
  94. m_CharacterController.height/2f, Physics.AllLayers, QueryTriggerInteraction.Ignore);
  95. desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
  96. m_MoveDir.x = desiredMove.x*speed;
  97. m_MoveDir.z = desiredMove.z*speed;
  98. if (m_CharacterController.isGrounded)
  99. {
  100. m_MoveDir.y = -m_StickToGroundForce;
  101. if (m_Jump)
  102. {
  103. m_MoveDir.y = m_JumpSpeed;
  104. PlayJumpSound();
  105. m_Jump = false;
  106. m_Jumping = true;
  107. }
  108. }
  109. else
  110. {
  111. m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
  112. }
  113. m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
  114. ProgressStepCycle(speed);
  115. UpdateCameraPosition(speed);
  116. m_MouseLook.UpdateCursorLock();
  117. }
  118. private void PlayJumpSound()
  119. {
  120. m_AudioSource.clip = m_JumpSound;
  121. m_AudioSource.Play();
  122. }
  123. private void ProgressStepCycle(float speed)
  124. {
  125. if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
  126. {
  127. m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
  128. Time.fixedDeltaTime;
  129. }
  130. if (!(m_StepCycle > m_NextStep))
  131. {
  132. return;
  133. }
  134. m_NextStep = m_StepCycle + m_StepInterval;
  135. PlayFootStepAudio();
  136. }
  137. private void PlayFootStepAudio()
  138. {
  139. if (!m_CharacterController.isGrounded)
  140. {
  141. return;
  142. }
  143. // pick & play a random footstep sound from the array,
  144. // excluding sound at index 0
  145. int n = Random.Range(1, m_FootstepSounds.Length);
  146. m_AudioSource.clip = m_FootstepSounds[n];
  147. m_AudioSource.PlayOneShot(m_AudioSource.clip);
  148. // move picked sound to index 0 so it's not picked next time
  149. m_FootstepSounds[n] = m_FootstepSounds[0];
  150. m_FootstepSounds[0] = m_AudioSource.clip;
  151. }
  152. private void UpdateCameraPosition(float speed)
  153. {
  154. Vector3 newCameraPosition;
  155. if (!m_UseHeadBob)
  156. {
  157. return;
  158. }
  159. if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
  160. {
  161. m_Camera.transform.localPosition =
  162. m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
  163. (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
  164. newCameraPosition = m_Camera.transform.localPosition;
  165. newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
  166. }
  167. else
  168. {
  169. newCameraPosition = m_Camera.transform.localPosition;
  170. newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
  171. }
  172. m_Camera.transform.localPosition = newCameraPosition;
  173. }
  174. private void GetInput(out float speed)
  175. {
  176. // Read input
  177. float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
  178. float vertical = CrossPlatformInputManager.GetAxis("Vertical");
  179. bool waswalking = m_IsWalking;
  180. #if !MOBILE_INPUT
  181. // On standalone builds, walk/run speed is modified by a key press.
  182. // keep track of whether or not the character is walking or running
  183. m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
  184. #endif
  185. // set the desired speed to be walking or running
  186. speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
  187. m_Input = new Vector2(horizontal, vertical);
  188. // normalize input if it exceeds 1 in combined length:
  189. if (m_Input.sqrMagnitude > 1)
  190. {
  191. m_Input.Normalize();
  192. }
  193. // handle speed change to give an fov kick
  194. // only if the player is going to a run, is running and the fovkick is to be used
  195. if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
  196. {
  197. StopAllCoroutines();
  198. StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
  199. }
  200. }
  201. private void RotateView()
  202. {
  203. m_MouseLook.LookRotation (transform, m_Camera.transform);
  204. }
  205. private void OnControllerColliderHit(ControllerColliderHit hit)
  206. {
  207. Rigidbody body = hit.collider.attachedRigidbody;
  208. //dont move the rigidbody if the character is on top of it
  209. if (m_CollisionFlags == CollisionFlags.Below)
  210. {
  211. return;
  212. }
  213. if (body == null || body.isKinematic)
  214. {
  215. return;
  216. }
  217. body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
  218. }
  219. public bool IsQuiet
  220. {
  221. get
  222. {
  223. if (!m_Jumping && m_CharacterController.velocity.magnitude <= 0)
  224. return true;
  225. else
  226. return false; } }
  227. public bool IsWalking
  228. {
  229. get
  230. {
  231. if (!m_Jumping && m_IsWalking && m_CharacterController.velocity.magnitude > 0)
  232. return true;
  233. else
  234. return false; } }
  235. public bool IsJumping
  236. {
  237. get
  238. {
  239. return m_Jumping;
  240. } }
  241. public bool IsRunning
  242. {
  243. get
  244. {
  245. if (!m_Jumping && !m_IsWalking && m_CharacterController.velocity.magnitude >= 0)
  246. return true;
  247. else
  248. return false; } }
  249. }
  250. }