using UnityEngine; using UnityEngine.InputSystem; [RequireComponent(typeof(CharacterController))] public class PlayerMovement : MonoBehaviour { [SerializeField] private float movementSpeed = 5f; [SerializeField] private float rotationSpeed = 200f; [SerializeField] private InputActionReference movement; [SerializeField] private Transform cameraTransform; [SerializeField] private Vector3 cameraOffset = new Vector3(0, 10, -10); private Vector3 inputDirection = Vector3.zero; private CharacterController controller; void LateUpdate() { // Keep the camera centered on the player at a fixed isometric offset if (cameraTransform != null) { cameraTransform.position = transform.position + cameraOffset; cameraTransform.LookAt(transform.position); } } void Start() { controller = GetComponent(); movement.action.Enable(); movement.action.performed += OnMovement; movement.action.canceled += OnMovementStop; } void OnMovement(InputAction.CallbackContext context) { Vector2 inputVector = context.ReadValue(); inputDirection = new Vector3(inputVector.x, 0, inputVector.y); } void OnMovementStop(InputAction.CallbackContext context) { inputDirection = Vector3.zero; } void Update() { Cursor.lockState = CursorLockMode.Locked; // Move the player Vector3 move = inputDirection.normalized * movementSpeed; controller.SimpleMove(move); // Rotate the player toward movement direction if (inputDirection != Vector3.zero) { Quaternion toRotation = Quaternion.LookRotation(inputDirection, Vector3.up); transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed*Time.deltaTime); } } }