# Finding distance of player to a path in 3D

I have a path in 3D (sort of a travel in space situation - Go from planet A to planet B). At the beginning of the game I show the path to be followed by the player using a thin line. The player is supposed to stay as close to the line as possible. If they move more than a certain distance x from the path, the player should automatically start moving towards the center.

For example - if the path is circular, and the player moves away from the circle, I want to calculate how far the player is from the path, then find the normal direction of the player to the circle, and push the player along that vector.

How do I find the distance of the player from the path? Keep in mind I need the shortest distance. I can’t really find the distance of the player to each point on the path and then use the shortest one; as it will be very expensive. Is there a way to do this? Also, how do I find the direction vector?

Thank you

This problem is quite trivial for a single linear line path, but not that easy or even impossible to get an exact point for a curved path depending on where the player is in relation to the curve. A curved path can have certain points which have the same distance to a given point. For example a perfect circle. At the center all points on the path have the same distance.

The usual approach is to simply project your point onto the line / line-segment which gives you the closest point on that line segment. So you just iterate through all line segments and calculate the closest point oo that segment. In the end you just pick the closest point.

Projecting a point onto a vector does not restrict the point to be between the start and end point of that line segment. However by “normalizing” the result in a specific way you get back a float value that is between 0 and 1 when the point is between that line segment. If you clamp that value to the range between 0 and 1 you always get the closest point to that given line segment.

``````//C#
public static Vector3 FindClosestPointOnLineSegment(Vector3 lineStart, Vector3 lineEnd, Vector3 point)
{
Vector3 line = lineEnd - lineStart;
Vector3 dir = point - lineStart;
float d = Vector3.Dot(line, dir) / line.sqrMagnitude;
d  = Mathf.Clamp01(d);
return Vector3.Lerp( lineStart, lineEnd, d);
}
``````

Keep in mind when the player is on the inside of a curved path the closest point could be behind the player and even further away from the target / end of the path than his current position. You might want to additionally weight the different points you get based on how far they are from the end and pick one that would be a compromise between being close to the path and still heading towards the target. Creating a reliable system highly depends on the possible shapes of your path.