Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class PlayerMovement : MonoBehaviour
- {
- [Header("Input Handlers")]
- [SerializeField] Transform orientation;
- [Header("Scripts")]
- [SerializeField] PlayerInputScript playerInput;
- [SerializeField] PlayerGroundCheck playerGroundCheck;
- [Header("Player Ground Values")]
- [SerializeField] float moveSpeed;
- //[SerializeField] float speedMult;
- [SerializeField] float groundDrag;
- [SerializeField] float airDrag;
- [Header("Player Air Values")]
- [SerializeField] float jumpForce;
- [SerializeField] float airMultiplier;
- // components
- private Rigidbody rb;
- private Vector3 moveDir;
- private bool isPlayerGrounded;
- private void Awake()
- {
- rb = GetComponent<Rigidbody>();
- //rb.freezeRotation = true;
- }
- private void FixedUpdate()
- {
- UpdatePlayerGrounded();
- HandleMovement();
- HandleAirDrag();
- SpeedClamper();
- }
- private void UpdatePlayerGrounded()
- {
- isPlayerGrounded = playerGroundCheck.GetIsPlayerGrounded();
- //Debug.Log("Player grounded: " + isPlayerGrounded);
- }
- public void OnJump(InputValue value)
- {
- //Debug.Log("Jump pressed with val: " + value.isPressed);
- HandleJump();
- }
- private void HandleJump()
- {
- //print("Is player grounded" + isPlayerGrounded);
- if (isPlayerGrounded)
- {
- rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z); // set to 0
- rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
- //print("Force added: " + transform.up * jumpForce * Time.deltaTime);
- }
- }
- private void HandleAirDrag()
- {
- if (isPlayerGrounded)
- {
- //Debug.Log("Lin dampening");
- rb.linearDamping = groundDrag;
- } else
- {
- rb.linearDamping = airDrag;
- }
- }
- private void HandleMovement()
- {
- float airMultiplierApply = isPlayerGrounded ? 1 : airMultiplier;
- Vector3[] camDirections = GetCamDirections();
- moveDir = (camDirections[0] * playerInput.GetMoveInput().y) + (camDirections[1] * playerInput.GetMoveInput().x);
- //Debug.Log("player air mult: " + airMultiplierApply);
- rb.AddForce(moveDir * moveSpeed * 10f * Time.deltaTime * airMultiplierApply, ForceMode.VelocityChange); // XZ movement
- this.transform.rotation = Quaternion.LookRotation(camDirections[0]); // camera
- }
- // helper methods
- private Vector3[] GetCamDirections()
- {
- Vector3 camForward = orientation.forward;
- Vector3 camRight = orientation.right;
- camForward.y = 0;
- camRight.y = 0;
- camForward.Normalize();
- camRight.Normalize();
- return new Vector3[] { camForward, camRight };
- }
- private void SpeedClamper()
- {
- Vector3 flatVel = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
- Debug.Log("Flat vel magnitude: " + flatVel.magnitude);
- if (flatVel.magnitude > moveSpeed)
- {
- Vector3 limitedVel = flatVel.normalized * moveSpeed;
- print("Limited vel: " + limitedVel.magnitude);
- rb.linearVelocity = new Vector3(limitedVel.x, rb.linearVelocity.y, limitedVel.z);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment