Advertisement
Guest User

Untitled

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