# How to Determine if a Waypoint has been Reached?

How can I find out if a waypoint has been reached?

Most examples use a distance check method :

``````if ( ( currentWaypoint - transform.position ).sqrMagnitude < presetDistance * presetDistance ) // sqrMagnitude, magnitude, Vector3.Distance, whatever your choice is
{
waypoint ++;
}
``````

However, if your NPC is moving fast or cannot turn sharply, the NPC will just circle the waypoint without reaching it.

So I developed another method where the distance to the waypoint is stored, next frame if the current distance is greater than the last distance, the NPC is moving away therefore the waypoint has been reached :

``````var dist : float = ( currentWaypoint - transform.position ).sqrMagnitude;

if ( dist > lastDist )
{
waypoint ++;
lastDist = Mathf.infinity;
}
else
{
lastDist = dist;
}
``````

This immediately breaks if the NPC is (or was) travelling in the opposite direction to the next waypoint. So I only start checking the distance if the NPC is close to the waypoint, giving it time to turn and travel towards the waypoint.

``````var dist : float = ( currentWaypoint - transform.position ).sqrMagnitude;

if ( dist < rangeToStartChecking )
{
if ( dist > lastDist )
{
waypoint ++;
lastDist = Mathf.infinity;
}
else
{
lastDist = dist;
}
}
else
{
lastDist = Mathf.infinity;
}
``````

This is actually the most foolproof method, as you don’t have to mess around with the sensitivity of presetDistance in the first distance-to-waypoint-check example.

However this is far from perfect so I ask the community :

How do I determine if a waypoint has been reached?

Many thanks =]

I would either stick with the first method and improve my NPC to allow it to slow down and not continuously orbit the waypoint, or simply extend the area of the waypoint or distance check in your case…

But… it really does depend on what your game is and what behaviour you want your NPCs to have. If they are people walking around then it’s reasonable for them to slow down to approach the waypoint. If your game is a racing game however then that’s probably not the desired behaviour as it’ll make your AI opponents slow and easy to beat…

Actually, this question was prompted by my space-shooter and racing games, so the velocity and turn rate are major factors.

But even with slow moving objects, having them stop within a threshold to the waypoint then having to manually lerp to the target position is a little annoying.

Is there no way to determine a waypoint has been reached, as close as the gameObject can get to it, considering its velocity and turn rate?

Edit: Never mind, thought of something else

Allow a rather big distance threshold as check.

Typically, if you’re moving along a path you’ll want to create a steering behavior that moves towards the “current” point on the path. The general consensus seems to be to do a “seek” to each point in the path until the last node is the one the object is moving towards, at which point you do an “arrive”. Arrive is almost the same as Seek except that Arrive attempts to move to the point so that the object has a zero velocity when the point is reached.

You can get a bit more realism if you combine Seek with a distance check that starts the object moving towards the next point proportionate to how close it is to the current point. This can get tricky when moving in tight corridors though (the object can start trying to cut corners and get stuck).

If you run into a situation where it would be impossible for an object to reach a point simply by moving forward with a heading-aligned velocity then you’ll have to tweak your algorithm so the object is able to figure out that it needs to “back up” in order to achieve a heading that faces the point (think of trying to navigate a car to a point 1 meter to its left but the car cannot go in reverse. The car’s turn rate will never be able to get to the point regardless of the velocity)

You can lerp from your current position to the way point. Doing this saves you doing a distance check at all. You can even extend this into a spline / animation curve based system if you so desire. The issue you’ll have to deal with here is unit speed. You’ll need to calculate the initial distance between start and end to calculate how long your unit should take from start to finish. Then each frame you’ll lerp an amount based on this calculated time value.

Here’s how I fixed orbiting on a previous game. Scale your velocity based on the dot product of your current direction with a vector between you and your destination. Basically if you are pointed in the right direction, you move at full speed. If you aren’t aimed at it, your velocity is reduced. Your turn rate remains constant. When I did this, the guys would make it to their destination every time and it looked pretty natural and avoided doing a lot of ugly interpolation.

Something like this:

``````float VelocityScale( Vector3 currentPos, Vector3 currentDir, Vector3 destinationPos )
{
Vector3 idealDir = currentPos - destinationPos;
idealDir.Normalize();
float scale = Vector3.Dot( currentDir, idealDir );
return Mathf.Clamp( scale, 0.0f, 1.0f );
}
``````

You still need to use a distance threshold to detect when you are close enough to your destination.

Hi there, I know it’s been forever since you posted here, but can you post some example code maybe that demonstrates this?