Hello, I am calculating the movement speed of a navmesh agent like this:
float currentSpeed = 0;
Vector3 currentPosition = Vector.zero;
Vector3 lastFramePosition = Vector3.zero;
void Awake()
{
lastFramePosition = transform.position;
}
void Update()
{
currentPosition = transform.position;
float distance = Vector3.Distance(lastFramePosition, currentPosition);
lastFramePosition = currentPosition;
currentSpeed = Mathf.Abs(distance) / Time.deltaTime;
}
then I tried to make the animation walk speed relative to the given currentSpeed this way:
if(currentSpeed > 0.1f)
{
animation["walk"].speed = currentSpeed / navMeshAgent.speed;
animation.CrossFade("walk");
}
else
{
animation.CrossFade("idle");
}
The speed of the walk animation drops and increases fine, but if the animation speed is lower than 1, it doesn't finish the loop. It just goes back to the first frame.
**Is there anything I can add to my calculation to make the walk animation loop correctly, regardless of the current speed?**
↧