Player keeps moving when it shouldn't

I two different forms of movement for my player, one for dodging and one for normal walking. Dodging works by simple using transform.translate and will stop moving after a certain distance. The normal walking uses navmeshes and navmeshagents, however, it does not stop when within a specified distance to the target. I can’t find why the navmeshagent isn’t stopping, but transform.translate will stop.

Here’s some code:

public float distanceToTarget;

void Update()
{
   distanceToTarget = Vector3.Distance(transform.position, target.transform.position);
}

void Dodging()
{
   if (dodging)
   {
      If (distanceToTarget > 1)
      {
         transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
      }
   }
}

void Walking()
{
   if (distanceToTarget > 3)
   {
      agent.SetDestination(target.transform.position);
   }
   else
   {
      agent.isStopped = true;
   }
}

So the Walking() function should keep updating the position of where to walk to as long as the player is more than 3 from the target, but, the player keeps moving towards the position of the target. For some reason it is completely ignoring the else statement when the distance is less than 3. I’ve set the distanceToTarget as public so I can watch the number go down, but even when it crosses the threshold of 3 it still keeps going down.

Does anyone know what is going on?

It seems to be directly related to the speed of the agent. The base speed is at 3.5 but if I do nothing but increase the speed to 6, my player doesn’t stop moving even when the distance is less than 3.

Is there a faster way to turn off the navmeshagent?

It would appear that setting velocity to 0 before stopping is a close enough way to make it work.

You can also enable/disable the agent, which seems to be much more abrupt of a movement so if you need nearly exact motion that would be the way to go.

Anyway here’s what I did if anyone needs to know:

agent.velocity = Vector3.zero;
agent.isStopped = true;

gets it close enough for my liking!

You set the destination to the target when distance is over 3, but the destination isn’t reset if it is under 3. As long as it has an destination, it will keep walking.