Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerScript : MonoBehaviour {
  6.  
  7.  
  8.  
  9. //Movement
  10. CharacterController player;
  11. public GameObject FPSCam;
  12. public float Sensitivity = 5f;
  13. [SerializeField]
  14. private float SpeedFB = 350;
  15. [SerializeField]
  16. private float SpeedLR = 250;
  17. private bool hasJumped, isCrouched, isSprinting;
  18. private float moveFB, moveLR, rotX, rotY, verticalVelocity, jumpForce = 5f;
  19.  
  20.  
  21.  
  22. void Start ()
  23. {
  24. player = this.GetComponent<CharacterController>();
  25. Cursor.lockState = CursorLockMode.Locked;
  26.  
  27. SpeedFB = 350;
  28. SpeedLR = 250;
  29. }
  30.  
  31.  
  32. private void Update ()
  33. {
  34. Movement();
  35. Sprint();
  36. Jump();
  37. Crouch();
  38. ApplyGravity();
  39. }
  40.  
  41.  
  42. private void Movement ()
  43. {
  44. moveFB = Input.GetAxis("Vertical") * SpeedFB * Time.deltaTime;
  45. moveLR = Input.GetAxis("Horizontal") * SpeedLR * Time.deltaTime;
  46. rotX = Input.GetAxis("Mouse X") * Sensitivity * Time.deltaTime;
  47. rotY = Input.GetAxis("Mouse Y") * Sensitivity * Time.deltaTime;
  48.  
  49. Vector3 movement = new Vector3(moveLR, verticalVelocity, moveFB);
  50. movement = transform.rotation * movement;
  51. transform.Rotate(0, rotX, 0);
  52. FPSCam.transform.Rotate(-rotY, 0, 0);
  53. player.Move(movement * Time.deltaTime);
  54. }
  55.  
  56.  
  57. private void Sprint()
  58. {
  59. if (Input.GetButtonDown("Sprint"))
  60. {
  61. if (isCrouched)
  62. {
  63. player.height = 2f;
  64. isCrouched = false;
  65. SpeedFB = 350;
  66. SpeedLR = 250;
  67. }
  68. if(isSprinting == false)
  69. {
  70. isSprinting = true;
  71. player.height = 2f;
  72. SpeedFB = SpeedFB * 1.5f;
  73. SpeedLR = SpeedLR * 1.25f;
  74. }
  75. }
  76. if (Input.GetButtonUp("Sprint"))
  77. {
  78. if (isCrouched)
  79. {
  80. isSprinting = false;
  81. SpeedFB = 175f;
  82. SpeedLR = 125f;
  83. }
  84. else
  85. {
  86. isSprinting = false;
  87. SpeedFB = 350f;
  88. SpeedLR = 250f;
  89. }
  90. }
  91. }
  92.  
  93.  
  94. private void Crouch()
  95. {
  96. if (Input.GetButtonDown("Crouch"))
  97. {
  98. if(isCrouched == false)
  99. {
  100. isCrouched = true;
  101. isSprinting = false;
  102. player.height = player.height / 2;
  103. SpeedFB = 175;
  104. SpeedLR = 125;
  105. }
  106. }
  107. if (Input.GetButtonUp("Crouch"))
  108. {
  109. if (isSprinting)
  110. {
  111. player.height = 2f;
  112. isCrouched = false;
  113. }
  114. else
  115. {
  116. player.height = 2f;
  117. isCrouched = false;
  118. SpeedFB = 350;
  119. SpeedLR = 250;
  120. }
  121. }
  122. }
  123.  
  124.  
  125. private void Jump()
  126. {
  127. if (Input.GetButtonDown("Jump"))
  128. {
  129. hasJumped = true;
  130. }
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137. private void ApplyGravity()
  138. {
  139. if (player.isGrounded)
  140. {
  141. if(hasJumped == false)
  142. {
  143. verticalVelocity = Physics.gravity.y;
  144. }
  145. else
  146. {
  147. verticalVelocity = jumpForce;
  148. }
  149. }
  150. else
  151. {
  152. verticalVelocity += Physics.gravity.y * Time.deltaTime;
  153. verticalVelocity = Mathf.Clamp(verticalVelocity, -12f, jumpForce);
  154. hasJumped = false;
  155. }
  156. }
  157.  
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement