Advertisement
Guest User

Untitled

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