using System; using UnityEngine; using UnityEngine.AI; using UnityEngine.EventSystems; public class PlayerMovement : MonoBehaviour { [Header("Components")] [SerializeField] private Player player; [SerializeField] private NavMeshAgent agent; private float lookRotationSpeed = 8f; private bool isMoving = false; private bool prepareEvent = false; private Action onDestinationReached; private Animator animator => player.Animator; public void MoveTo(Vector3 point, Action action = null) { agent.destination = point; onDestinationReached = action; } public void MoveToBlock(Vector3 point, Action action = null) { InteractionManager.Instance.AddBlockReason("Moving", false); MoveTo(point, () => { action?.Invoke(); InteractionManager.Instance.RemoveBlockReason("Moving"); }); } private void Update() { isMoving = !agent.pathPending && agent.remainingDistance > agent.stoppingDistance; if (isMoving) { prepareEvent = true; FaceTarget(); } else if (prepareEvent) { prepareEvent = false; onDestinationReached?.Invoke(); } SetAnimations(); } private void FaceTarget() { if (agent.velocity == Vector3.zero) return; Vector3 direction = (agent.destination - transform.position).normalized; Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z)); transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * lookRotationSpeed); } public void StopMovement() { agent.destination = transform.position; } private void SetAnimations() { if (!animator) return; animator.SetBool("Walking", agent.velocity != Vector3.zero); } }