using UnityEngine; using UnityEngine.InputSystem; public class DabouzaMovement : MonoBehaviour { [Header("Movement")] public float moveSpeed; [Header("References")] public Transform orientation; float horizontalInput; float verticalInput; Vector3 moveDirection; Rigidbody rb; private void Start() { rb = GetComponent(); rb.freezeRotation = true; // stop the Rigidbody from tipping over from physics collisions } private void Update() { MyInput(); } private void FixedUpdate() // physics updates should live here, not in Update() { 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; } private void MovePlayer() { // combine forward/back and left/right based on where "orientation" is facing moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput; rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force); } }