Add project files.
This commit is contained in:
42
Assets/Scripts/Movement/Mover.cs
Normal file
42
Assets/Scripts/Movement/Mover.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Movement/Mover.cs.meta
Normal file
2
Assets/Scripts/Movement/Mover.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 99048001b4ebc1543911415b71049d4a
|
||||
Reference in New Issue
Block a user