Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.17 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.  
  14. /*New*/
  15. [SerializeField] private float crouchHeight;
  16. [SerializeField] private float standHeight;
  17. [SerializeField] public bool crouching;
  18. [SerializeField] private float m_CrouchSpeed;
  19.  
  20. [SerializeField] private bool m_IsWalking;
  21. [SerializeField] private float m_WalkSpeed;
  22. /*End New*/
  23.  
  24. [SerializeField] private float m_RunSpeed;
  25. [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
  26. [SerializeField] private float m_JumpSpeed;
  27. [SerializeField] private float m_StickToGroundForce;
  28. [SerializeField] private float m_GravityMultiplier;
  29. [SerializeField] private MouseLook m_MouseLook;
  30. [SerializeField] private bool m_UseFovKick;
  31. [SerializeField] private FOVKick m_FovKick = new FOVKick();
  32. [SerializeField] private bool m_UseHeadBob;
  33. [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
  34. [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
  35. [SerializeField] private float m_StepInterval;
  36. [SerializeField] private AudioClip[] m_FootstepSounds; // an array of footstep sounds that will be randomly selected from.
  37. [SerializeField] private AudioClip m_JumpSound; // the sound played when character leaves the ground.
  38. [SerializeField] private AudioClip m_LandSound; // the sound played when character touches back on ground.
  39.  
  40. /*New*/
  41. private float m_MaxRunSpeed;
  42. private float m_MaxWalkSpeed;
  43. private bool f_Crouch;
  44. /*End new*/
  45.  
  46. private float currSpeed;
  47. private Camera m_Camera;
  48. private bool m_Jump;
  49. private float m_YRotation;
  50. private Vector2 m_Input;
  51. private Vector3 m_MoveDir = Vector3.zero;
  52. private CharacterController m_CharacterController;
  53. private CollisionFlags m_CollisionFlags;
  54. private bool m_PreviouslyGrounded;
  55. private Vector3 m_OriginalCameraPosition;
  56. private float m_StepCycle;
  57. private float m_NextStep;
  58. private bool m_Jumping;
  59. private AudioSource m_AudioSource;
  60.  
  61. // Use this for initialization
  62. private void Start()
  63. {
  64. m_CharacterController = GetComponent<CharacterController>();
  65. m_Camera = Camera.main;
  66. m_OriginalCameraPosition = m_Camera.transform.localPosition;
  67. m_FovKick.Setup(m_Camera);
  68. m_HeadBob.Setup(m_Camera, m_StepInterval);
  69. m_StepCycle = 0f;
  70. m_NextStep = m_StepCycle/2f;
  71. m_Jumping = false;
  72. m_AudioSource = GetComponent<AudioSource>();
  73. m_MouseLook.Init(transform , m_Camera.transform);
  74.  
  75. m_MaxRunSpeed = m_RunSpeed;
  76. m_MaxWalkSpeed = m_WalkSpeed;
  77. crouchHeight = 0.1f;
  78. standHeight = m_CharacterController.height;
  79. forceCrouch = false;
  80.  
  81. }
  82.  
  83.  
  84. // Update is called once per frame
  85. private void Update()
  86. {
  87. RotateView();
  88. // the jump state needs to read here to make sure it is not missed
  89. if (!m_Jump)
  90. {
  91. m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
  92. }
  93.  
  94. if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
  95. {
  96. StartCoroutine(m_JumpBob.DoBobCycle());
  97. PlayLandingSound();
  98. m_MoveDir.y = 0f;
  99. m_Jumping = false;
  100. }
  101. if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
  102. {
  103. m_MoveDir.y = 0f;
  104. }
  105. /*
  106. * Checks to make sure there's no boolean conflicts between being forced to crouch
  107. * and the player choosing to crouch
  108. */
  109. if(m_CharacterController.isGrounded && !m_Jump){
  110. #if !MOBILE_INPUT
  111. if(forceCrouch||Input.GetKey(KeyCode.C)){
  112. crouch (true);
  113. // Debug.LogWarning (forceCrouch);
  114. }
  115. else {
  116. crouch (false);
  117. // Debug.LogWarning ("standing");
  118. }
  119. #endif
  120. }
  121.  
  122.  
  123. m_PreviouslyGrounded = m_CharacterController.isGrounded;
  124. }
  125.  
  126.  
  127. private void PlayLandingSound()
  128. {
  129. m_AudioSource.clip = m_LandSound;
  130. m_AudioSource.Play();
  131. m_NextStep = m_StepCycle + .5f;
  132. }
  133.  
  134.  
  135. private void FixedUpdate()
  136. {
  137. float speed;
  138. GetInput(out speed);
  139. // always move along the camera forward as it is the direction that it being aimed at
  140. Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
  141.  
  142. // get a normal for the surface that is being touched to move along it
  143. RaycastHit hitInfo;
  144. Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
  145. m_CharacterController.height/2f);
  146. desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
  147.  
  148. m_MoveDir.x = desiredMove.x*speed;
  149. m_MoveDir.z = desiredMove.z*speed;
  150.  
  151.  
  152. if (m_CharacterController.isGrounded)
  153. {
  154. m_MoveDir.y = -m_StickToGroundForce;
  155.  
  156. if (m_Jump)
  157. {
  158. m_MoveDir.y = m_JumpSpeed;
  159. PlayJumpSound();
  160. m_Jump = false;
  161. m_Jumping = true;
  162. }
  163. }
  164. else
  165. {
  166. m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
  167. }
  168. m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
  169.  
  170. ProgressStepCycle(speed);
  171. UpdateCameraPosition(speed);
  172.  
  173.  
  174.  
  175. }
  176.  
  177.  
  178. private void PlayJumpSound()
  179. {
  180. m_AudioSource.clip = m_JumpSound;
  181. m_AudioSource.Play();
  182. }
  183.  
  184.  
  185. private void ProgressStepCycle(float speed)
  186. {
  187. if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
  188. {
  189. m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
  190. Time.fixedDeltaTime;
  191. }
  192.  
  193. if (!(m_StepCycle > m_NextStep))
  194. {
  195. return;
  196. }
  197.  
  198. m_NextStep = m_StepCycle + m_StepInterval;
  199.  
  200. PlayFootStepAudio();
  201. }
  202.  
  203.  
  204. private void PlayFootStepAudio()
  205. {
  206. if (!m_CharacterController.isGrounded)
  207. {
  208. return;
  209. }
  210. // pick & play a random footstep sound from the array,
  211. // excluding sound at index 0
  212. int n = Random.Range(1, m_FootstepSounds.Length);
  213. m_AudioSource.clip = m_FootstepSounds[n];
  214. m_AudioSource.PlayOneShot(m_AudioSource.clip);
  215. // move picked sound to index 0 so it's not picked next time
  216. m_FootstepSounds[n] = m_FootstepSounds[0];
  217. m_FootstepSounds[0] = m_AudioSource.clip;
  218. }
  219.  
  220.  
  221. private void UpdateCameraPosition(float speed)
  222. {
  223. Vector3 newCameraPosition;
  224. if (!m_UseHeadBob)
  225. {
  226. return;
  227. }
  228. if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded && !crouching)
  229. {
  230. m_Camera.transform.localPosition =
  231. m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
  232. (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
  233. newCameraPosition = m_Camera.transform.localPosition;
  234. newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
  235. }
  236. else if(crouching){
  237. newCameraPosition = m_Camera.transform.localPosition;
  238. newCameraPosition.y = crouchHeight+1f;
  239. // Debug.Log ("hit");
  240. }
  241. else
  242. {
  243. newCameraPosition = m_Camera.transform.localPosition;
  244. newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
  245.  
  246. }
  247. m_Camera.transform.localPosition = newCameraPosition;
  248. }
  249.  
  250.  
  251. private void GetInput(out float speed)
  252. {
  253.  
  254. // Read input
  255. float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
  256. float vertical = CrossPlatformInputManager.GetAxis("Vertical");
  257.  
  258. bool waswalking = m_IsWalking;
  259.  
  260. #if !MOBILE_INPUT
  261. // On standalone builds, walk/run speed is modified by a key press.
  262. // keep track of whether or not the character is walking or running
  263. m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
  264.  
  265.  
  266.  
  267. #endif
  268.  
  269. if(crouching){
  270. speed = m_CrouchSpeed;
  271. }
  272. else{
  273. speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
  274. }
  275. // if(crouching){
  276. // crouch (true);
  277. // speed = m_CrouchSpeed;
  278. // }
  279. // else{
  280. // crouch (false);
  281. // speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
  282. // }
  283.  
  284.  
  285.  
  286. m_Input = new Vector2(horizontal, vertical);
  287.  
  288. // normalize input if it exceeds 1 in combined length:
  289. if (m_Input.sqrMagnitude > 1)
  290. {
  291. m_Input.Normalize();
  292. }
  293.  
  294. // handle speed change to give an fov kick
  295. // only if the player is going to a run, is running and the fovkick is to be used
  296. if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
  297. {
  298. StopAllCoroutines();
  299. StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
  300. }
  301. }
  302.  
  303.  
  304. private void RotateView()
  305. {
  306. m_MouseLook.LookRotation (transform, m_Camera.transform);
  307. }
  308.  
  309.  
  310. private void OnControllerColliderHit(ControllerColliderHit hit)
  311. {
  312. Rigidbody body = hit.collider.attachedRigidbody;
  313. //dont move the rigidbody if the character is on top of it
  314. if (m_CollisionFlags == CollisionFlags.Below)
  315. {
  316. return;
  317. }
  318.  
  319. if (body == null || body.isKinematic)
  320. {
  321. return;
  322. }
  323. body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
  324. }
  325.  
  326.  
  327. /*Rescales the player so that it appears that they are crouching*/
  328. public void crouch(bool value) {
  329. // Debug.Log (value);
  330. Vector3 scale = transform.localScale;
  331.  
  332. switch(value){
  333. case true:
  334. crouching = true;
  335.  
  336. scale = new Vector3(crouchHeight,crouchHeight,crouchHeight);
  337. transform.localScale = scale;
  338.  
  339. break;
  340.  
  341. case false:
  342. crouching = false;
  343.  
  344.  
  345.  
  346. scale = new Vector3(.7f,.7f,.7f);
  347. transform.localScale = scale;
  348.  
  349. break;
  350. }
  351. }
  352.  
  353. /*Used to force the player into a crouch position should it be needed*/
  354. public bool forceCrouch
  355. {
  356. get{return f_Crouch;}
  357. set{f_Crouch = value;}
  358. }
  359.  
  360. /*Disables running, should it be needed*/
  361. public void IsWalking(bool value)
  362. {
  363. if(value)
  364. m_RunSpeed = m_MaxWalkSpeed;
  365. else{
  366. m_RunSpeed = m_MaxRunSpeed;
  367. }
  368. }
  369. /*End new*/
  370. }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement