Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(CharacterController))]
  6. public class PlayerMovement : MonoBehaviour {
  7.  
  8. private CharacterController character;
  9. [SerializeField] private GameObject playerCam;
  10.  
  11. //Movement variables
  12. private float moveFB, moveLR;
  13. [SerializeField] private float speedFB, speedLR;
  14.  
  15. //Mouse variables
  16. private float rotX, rotY;
  17. public float mouseSensitivity = 3;
  18.  
  19. //Physics/Jump/Sprinting/Crouching variables
  20.  
  21. [SerializeField] private bool hasJumped;
  22. [SerializeField] private bool canJump;
  23. [SerializeField] private float jumpForce;
  24.  
  25. [SerializeField] private float sprintingSpeedFB = 6;
  26. [SerializeField] private float sprintingSpeedLR = 5;
  27. [SerializeField] private bool isSprinting;
  28.  
  29. [SerializeField] private float crouchedSpeedFB = 4;
  30. [SerializeField] private float crouchedSpeedLR = 2.5f;
  31. [SerializeField] private bool isCrouched;
  32. [SerializeField] private int characterHeightWhenCrouched = 1;
  33.  
  34. /*The speed at which the player falls down. It's normally set to 9.81f by the Physics.Gravity.y function
  35. * in line 100*/
  36. [SerializeField] private float verticalVelocity;
  37.  
  38.  
  39. private void Awake()
  40. {
  41. Cursor.lockState = CursorLockMode.Locked;
  42. }
  43.  
  44. // Use this for initialization
  45. private void Start ()
  46. {
  47. //Setting V-Sync to 0
  48. QualitySettings.vSyncCount = 0;
  49.  
  50. //Variables
  51. speedFB = 5;
  52. speedLR = 3.5f;
  53.  
  54. character = this.GetComponent<CharacterController>();
  55. }
  56.  
  57. // Update is called once per frame
  58. private void Update ()
  59. {
  60. Movement();
  61. ApplyGravity();
  62. Sprint();
  63. Crouch();
  64. }
  65.  
  66. private void Movement()
  67. {
  68. moveFB = Input.GetAxis("Vertical") * speedFB;
  69. moveLR = Input.GetAxis("Horizontal") * speedLR;
  70.  
  71. rotY -= Input.GetAxisRaw("Mouse Y") * mouseSensitivity;
  72. rotX = Input.GetAxisRaw("Mouse X") * mouseSensitivity;
  73.  
  74. Vector3 movement = new Vector3(moveLR, verticalVelocity, moveFB);
  75.  
  76. transform.Rotate(0, rotX, 0);
  77. //Only god knows what happens here. It works, so I won't touch it. Let's just not bring it up. Ever again.
  78. playerCam.transform.localRotation = Quaternion.Euler(rotY, 0, 0);
  79. rotY = Mathf.Clamp(rotY, -65f, 85f);
  80. movement = transform.rotation * movement;
  81.  
  82. character.Move(movement * Time.deltaTime);
  83.  
  84.  
  85. if (Input.GetButtonDown("Jump"))
  86. {
  87. Jump();
  88. }
  89. }
  90.  
  91. private void Jump()
  92. {
  93. hasJumped = true;
  94. }
  95.  
  96. private void ApplyGravity()
  97. {
  98. if (character.isGrounded)
  99. {
  100. if(hasJumped == false)
  101. {
  102. verticalVelocity = Physics.gravity.y;
  103. }
  104. else
  105. {
  106. verticalVelocity = jumpForce;
  107. }
  108. }
  109. else
  110. {
  111. verticalVelocity += Physics.gravity.y * Time.deltaTime;
  112. //clamping the falling speed
  113. verticalVelocity = Mathf.Clamp(verticalVelocity, -20, jumpForce);
  114. hasJumped = false;
  115. }
  116. }
  117.  
  118. private void Sprint()
  119. {
  120. if (Input.GetButton("Sprint"))
  121. {
  122. isSprinting = true;
  123. isCrouched = false;
  124. if (isSprinting)
  125. {
  126. speedFB = sprintingSpeedFB;
  127. speedLR = sprintingSpeedLR;
  128. }
  129. }
  130. else
  131. {
  132. isSprinting = false;
  133. if(isSprinting == false)
  134. {
  135. speedFB = 5;
  136. speedLR = 3.5f;
  137. }
  138.  
  139. }
  140. }
  141.  
  142. private void Crouch()
  143. {
  144. if (Input.GetButton("Crouch"))
  145. {
  146. isCrouched = true;
  147. if (isCrouched)
  148. {
  149. character.height = characterHeightWhenCrouched;
  150. speedFB = crouchedSpeedFB;
  151. speedLR = crouchedSpeedLR;
  152.  
  153. if (isSprinting)
  154. {
  155. character.height = 2;
  156. speedFB = sprintingSpeedFB;
  157. speedLR = sprintingSpeedLR;
  158. }
  159. }
  160. }
  161. else
  162. {
  163. isCrouched = false;
  164. if(isCrouched == false)
  165. {
  166. if (isSprinting)
  167. {
  168. character.height = 2;
  169. speedFB = sprintingSpeedFB;
  170. speedLR = sprintingSpeedLR;
  171. }
  172. else
  173. {
  174. character.height = 2;
  175. speedFB = 5;
  176. speedLR = 3.5f;
  177. }
  178. }
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement