using UnityEngine; using UnityEngine.InputSystem; public class DabouzaMovement : MonoBehaviour { [Header("Movement")] public float moveSpeed; public float groundDrag; [Header("Jumping")] public float jumpForce; public float jumpCooldown; bool readyToJump = true; public Key jumpKey = Key.Space; [Header("Ground Check")] public float playerHeight; public LayerMask whatIsGround; bool grounded; [Header("References")] public Transform orientation; float horizontalInput; float verticalInput; Vector3 moveDirection; Rigidbody rb; private void Start() { rb = GetComponent(); rb.freezeRotation = true; } private void Update() { grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround); MyInput(); rb.linearDamping = grounded ? groundDrag : 0f; } private void FixedUpdate() { MovePlayer(); } private void MyInput() { horizontalInput = 0f; verticalInput = 0f; if (Keyboard.current == null) return; if (Keyboard.current[Key.D].isPressed) horizontalInput = 1f; if (Keyboard.current[Key.A].isPressed) horizontalInput = -1f; if (Keyboard.current[Key.W].isPressed) verticalInput = 1f; if (Keyboard.current[Key.S].isPressed) verticalInput = -1f; if (Keyboard.current[jumpKey].wasPressedThisFrame && readyToJump && grounded) { readyToJump = false; Jump(); Invoke(nameof(ResetJump), jumpCooldown); } } private void MovePlayer() { moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput; rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force); } private void Jump() { rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z); rb.AddForce(transform.up * jumpForce, ForceMode.Impulse); } private void ResetJump() { readyToJump = true; } }