PlayerMovement.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class PlayerMovement : MonoBehaviour
  4. {
  5. [Header("Movement")]
  6. public float moveSpeed;
  7. public float groundDrag;
  8. public float airDrag;
  9. public float jumpForce;
  10. public float jumpCooldown;
  11. public float airMultiplier;
  12. bool readyToJump;
  13. public float sprintSpeed = 1.5f;
  14. public bool isSprinting;
  15. private float maxStamina = 1;
  16. private float minStamina = 0;
  17. public float stamina = 1;
  18. public Slider staminaBar;
  19. [Header("Keybinds")]
  20. public KeyCode jumpKey = KeyCode.Space;
  21. public KeyCode sprintKey = KeyCode.LeftShift;
  22. [Header("Ground Check")]
  23. public float playerHeight;
  24. public LayerMask whatIsGround;
  25. bool grounded;
  26. public Transform orientation;
  27. float horizontalInput;
  28. float verticalInput;
  29. Vector3 moveDirection;
  30. Rigidbody rb;
  31. private void Start()
  32. {
  33. SnapToGround();
  34. rb = GetComponent<Rigidbody>();
  35. rb.freezeRotation = true;
  36. readyToJump = true;
  37. isSprinting = false;
  38. }
  39. void SnapToGround()
  40. {
  41. RaycastHit hit;
  42. if (Physics.Raycast(transform.position, Vector3.down, out hit, 100, whatIsGround))
  43. {
  44. Vector3 pos = transform.position;
  45. pos.y = hit.point.y + 5;
  46. transform.position = pos;
  47. }
  48. }
  49. private void Update()
  50. {
  51. // ground check
  52. grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround);
  53. MyInput();
  54. if (isSprinting && stamina>minStamina)
  55. {
  56. stamina -= Time.deltaTime/4;
  57. }
  58. else if(!isSprinting && stamina < maxStamina)
  59. {
  60. stamina += Time.deltaTime;
  61. }
  62. staminaBar.value = stamina;
  63. // handle drag
  64. if (grounded)
  65. rb.linearDamping = groundDrag;
  66. else
  67. rb.linearDamping = airDrag;
  68. }
  69. private void FixedUpdate()
  70. {
  71. MovePlayer();
  72. }
  73. private void MyInput()
  74. {
  75. horizontalInput = Input.GetAxisRaw("Horizontal");
  76. verticalInput = Input.GetAxisRaw("Vertical");
  77. // when to jump
  78. if(Input.GetKey(jumpKey) && readyToJump && grounded)
  79. {
  80. readyToJump = false;
  81. Jump();
  82. Invoke(nameof(ResetJump), jumpCooldown);
  83. }
  84. if (Input.GetKey(sprintKey))
  85. {
  86. isSprinting = true;
  87. }
  88. else
  89. {
  90. isSprinting = false;
  91. }
  92. }
  93. private void MovePlayer()
  94. {
  95. // calculate movement direction
  96. moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
  97. if (isSprinting && stamina > 0)
  98. {
  99. // on ground
  100. if (grounded)
  101. rb.AddForce(moveDirection.normalized * moveSpeed * 10f * sprintSpeed, ForceMode.Force);
  102. // in air
  103. else if(!grounded)
  104. rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier * sprintSpeed, ForceMode.Force);
  105. }
  106. else
  107. {
  108. // on ground
  109. if (grounded)
  110. rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
  111. // in air
  112. else if (!grounded)
  113. rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
  114. }
  115. }
  116. private void Jump()
  117. {
  118. // reset y velocity
  119. rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
  120. rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
  121. }
  122. private void ResetJump()
  123. {
  124. readyToJump = true;
  125. }
  126. }