Guest User

Untitled

a guest
Jul 24th, 2026
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1.  
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5.  
  6. public class PlayerMovement : MonoBehaviour
  7. {
  8. [Header("Input Handlers")]
  9. [SerializeField] Transform orientation;
  10.  
  11. [Header("Scripts")]
  12. [SerializeField] PlayerInputScript playerInput;
  13. [SerializeField] PlayerGroundCheck playerGroundCheck;
  14.  
  15. [Header("Player Ground Values")]
  16. [SerializeField] float moveSpeed;
  17. //[SerializeField] float speedMult;
  18. [SerializeField] float groundDrag;
  19. [SerializeField] float airDrag;
  20.  
  21. [Header("Player Air Values")]
  22. [SerializeField] float jumpForce;
  23. [SerializeField] float airMultiplier;
  24.  
  25. // components
  26. private Rigidbody rb;
  27. private Vector3 moveDir;
  28. private bool isPlayerGrounded;
  29.  
  30. private void Awake()
  31. {
  32. rb = GetComponent<Rigidbody>();
  33. //rb.freezeRotation = true;
  34. }
  35.  
  36. private void FixedUpdate()
  37. {
  38. UpdatePlayerGrounded();
  39. HandleMovement();
  40. HandleAirDrag();
  41. SpeedClamper();
  42.  
  43. }
  44.  
  45. private void UpdatePlayerGrounded()
  46. {
  47. isPlayerGrounded = playerGroundCheck.GetIsPlayerGrounded();
  48. //Debug.Log("Player grounded: " + isPlayerGrounded);
  49. }
  50.  
  51. public void OnJump(InputValue value)
  52. {
  53. //Debug.Log("Jump pressed with val: " + value.isPressed);
  54. HandleJump();
  55. }
  56.  
  57. private void HandleJump()
  58. {
  59. //print("Is player grounded" + isPlayerGrounded);
  60. if (isPlayerGrounded)
  61. {
  62. rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z); // set to 0
  63. rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
  64. //print("Force added: " + transform.up * jumpForce * Time.deltaTime);
  65. }
  66.  
  67. }
  68.  
  69.  
  70. private void HandleAirDrag()
  71. {
  72. if (isPlayerGrounded)
  73. {
  74. //Debug.Log("Lin dampening");
  75. rb.linearDamping = groundDrag;
  76. } else
  77. {
  78. rb.linearDamping = airDrag;
  79. }
  80. }
  81.  
  82. private void HandleMovement()
  83. {
  84. float airMultiplierApply = isPlayerGrounded ? 1 : airMultiplier;
  85. Vector3[] camDirections = GetCamDirections();
  86. moveDir = (camDirections[0] * playerInput.GetMoveInput().y) + (camDirections[1] * playerInput.GetMoveInput().x);
  87. //Debug.Log("player air mult: " + airMultiplierApply);
  88. rb.AddForce(moveDir * moveSpeed * 10f * Time.deltaTime * airMultiplierApply, ForceMode.VelocityChange); // XZ movement
  89. this.transform.rotation = Quaternion.LookRotation(camDirections[0]); // camera
  90.  
  91.  
  92. }
  93.  
  94. // helper methods
  95. private Vector3[] GetCamDirections()
  96. {
  97. Vector3 camForward = orientation.forward;
  98. Vector3 camRight = orientation.right;
  99. camForward.y = 0;
  100. camRight.y = 0;
  101. camForward.Normalize();
  102. camRight.Normalize();
  103.  
  104. return new Vector3[] { camForward, camRight };
  105. }
  106.  
  107. private void SpeedClamper()
  108. {
  109. Vector3 flatVel = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
  110. Debug.Log("Flat vel magnitude: " + flatVel.magnitude);
  111. if (flatVel.magnitude > moveSpeed)
  112. {
  113. Vector3 limitedVel = flatVel.normalized * moveSpeed;
  114. print("Limited vel: " + limitedVel.magnitude);
  115. rb.linearVelocity = new Vector3(limitedVel.x, rb.linearVelocity.y, limitedVel.z);
  116.  
  117. }
  118.  
  119.  
  120. }
  121.  
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment