using UnityEngine; using UnityEngine.InputSystem; public class DabouzaMovement : MonoBehaviour { [Header("Movement")] public float moveSpeed; [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() { // ground check — do this in Update() so it's checked every frame, as early as possible grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround); MyInput(); } 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() { // zero out existing vertical velocity so jump height is consistent // whether the player was falling, standing still, or already moving up slightly //rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z); rb.AddForce(transform.up * jumpForce, ForceMode.Impulse); } private void ResetJump() { readyToJump = true; } }