- Home /
Navmeshagent Spins on the Spot when Close to Destination
I have a navmeshagent, and a set of destinations that the agent walks between. The agent always will reach the destination eventually, but sometimes when it gets pretty close, it will spin for up to 30 seconds before actually getting to the destination.
I have a feeling it's because the agent can't reach the destination exactly on the first time, so it's overshooting by a little bit every time, and that causes the spinning.
Any suggestions on how to fix this? I've tried tweaking the navmeshagent settings, but have had no luck.
Answer by skprsr · Oct 23, 2015 at 06:06 PM
You can try increasing the Stopping Distance of your NavMeshAgent from the inspector...
Sometime it can't calculate Stopping Distance exact 0 as remaining and then it start to make misbehave.
So make Stopping Distance something large enough.
Answer by howong · Mar 30, 2018 at 01:33 AM
Thanks all. I tried increasing the stopping distance, but even at low values like 0.1, I saw the issue still happen occasionally. I also tried the link supplied by CarterG81, but the solution makes the agent move while ignoring acceleration (it uses the max speed directly), and also the agent doesn't move in a curved fashion - the agent normally (when updatePosition is true) moves on a curve toward their steering target based on velocity, acceleration, and angular speed. I found that to preserve the acceleration (mostly) and curved movement (mostly), I move the agent straight toward the destination on the last stretch (on the last steering target). This retains the curved movement for the corners in between, then when the destination is the last "corner," we move straight toward it (the agent still rotates as they would when moving in a curved fashion, which in my opinion is better than an abrupt change in rotation, but I suppose one could make some changes to do an abrupt rotation change if they wanted). The desiredVelocity will always form a straight line toward the steeringTarget (as dictated by the path). This means though that there won't be any deceleration as the agent nears the destination and no acceleration if going straight from rest, so I'm working on fixing those aspects now. This is for the case that we don't use animation root motion for movement. I'm still looking into the root motion case.
Code:
// Avoid circling the destination
if (agent.hasPath && (agent.steeringTarget == agent.destination))
{
Debug.Log ("Has path and steering target is destination");
if (Mathf.Approximately (agent.velocity.magnitude, 0))
{
Debug.Log ("Velocity was zero, so using desired velocity (max speed)");
agent.velocity = agent.desiredVelocity;
}
else
{
Debug.Log ("Velocity was non-zero, so using desired velocity direction with velocity magnitude");
agent.velocity = (agent.desiredVelocity.normalized * agent.velocity.magnitude);
}
}
Answer by BrainSlugs83 · Apr 09, 2020 at 07:08 AM
I just tell it to stop if it gets close enough (note that you have to set .isStopped = false
the next time you call .SetDestination
):
if ((this.transform.position - agent.destination).magnitude < .1f)
{
Agent.isStopped = true;
}
I have no idea why this is necessary, but Stopping Distance
just didn't work for me. -- Might be because I'm using a RigidBody? -- But for whatever reason. I ended up setting Stopping Distance
to 0
, and manually checking for myself using the above code.