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

42 lines
1.1 KiB
C#
Raw Normal View History

2025-10-27 17:05:16 +00:00
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
namespace RPG.Movement
{
public class Mover : MonoBehaviour
{
[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();
}
public void MoveTo(Vector3 destination)
{
navMeshAgent.destination = destination;
}
private void UpdateAnimator()
{
// This is your original code, which is correct!
// It just uses the cached variables now.
Vector3 velocity = navMeshAgent.velocity;
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
float speed = localVelocity.z;
animator.SetFloat("forwardSpeed", speed);
}
}
}