Anon-77

Just a failed attempt at a double jump

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