Files
GoblinRaid/Assets/Scripts/Movement/Mover.cs

51 lines
1.3 KiB
C#
Raw Normal View History

2025-10-28 17:26:07 +00:00
using RPG.Core;
2025-10-27 17:05:16 +00:00
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
namespace RPG.Movement
{
2025-10-28 17:26:07 +00:00
public class Mover : MonoBehaviour, IAction
2025-10-27 17:05:16 +00:00
{
[SerializeField] private Transform target;
// Cache components for performance
private NavMeshAgent navMeshAgent;
private Animator animator;
private void Awake()
{
// Get component references once
navMeshAgent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
}
void Update()
{
UpdateAnimator();
}
2025-10-27 17:26:17 +00:00
public void StartMoveAction(Vector3 destination)
{
2025-10-28 17:26:07 +00:00
GetComponent<ActionScheduler>().StartAction(this);
2025-10-27 17:26:17 +00:00
MoveTo(destination);
}
2025-10-27 17:05:16 +00:00
public void MoveTo(Vector3 destination)
{
navMeshAgent.destination = destination;
2025-10-27 17:26:17 +00:00
navMeshAgent.isStopped = false;
}
2025-10-28 17:26:07 +00:00
public void Cancel()
2025-10-27 17:26:17 +00:00
{
navMeshAgent.isStopped = true;
2025-10-27 17:05:16 +00:00
}
private void UpdateAnimator()
{
Vector3 velocity = navMeshAgent.velocity;
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
float speed = localVelocity.z;
animator.SetFloat("forwardSpeed", speed);
}
}
}