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); [SerializeField] private Animator _animator; private Vector3 inputDirection = Vector3.zero; private CharacterController controller; void LateUpdate() { 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; Vector3 cameraForward = cameraTransform.forward; Vector3 cameraRight = cameraTransform.right; cameraForward.y = 0; cameraRight.y = 0; cameraForward.Normalize(); cameraRight.Normalize(); Vector3 move = (cameraForward * inputDirection.z + cameraRight * inputDirection.x).normalized * movementSpeed; controller.SimpleMove(move); print("ttesto"); _animator.SetFloat("Speed", move.magnitude); if (move.magnitude > 0.01f) { Quaternion toRotation = Quaternion.LookRotation(move, Vector3.up); transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime); } } }