NaN after dividing by Time.deltaTime

Hi!

I’m trying to calculate the speed of a NavMeshAgent to feed into my Animator component using this formula:

Vector3 currentMove = transform.position - previousPosition;
float currentSpeed = currentMove.magnitude / (0.000000001f + Time.deltaTime);

Problem is, when I pause using Time.timeScale = 0 it ends up dividing by 0, therefore returning error or failing.

My improvised fix is to add a very small ammount to prevent it from ever being 0, or I could check if it results in 0 an instead changing it for a very small ammount.

Either way it just doesn’t feel right. I did my research and couldn’t find a satisfying answer. Any idea what’s the best way to go about it?

I not sure why you will get NaN, but another workaround is set the currentspeed to 0 whenever your timescale is 0

float currentSpeed = 0f;

if(Time.timeScale > 0f)
{
    Vector3 currentMove = transform.position - previousPosition; 
    currentSpeed = currentMove.magnitude / Time.deltaTime;
}