- Home /

# 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

Maybe try to iterete over the path points with some step offset like:

int offset = 10;

for( int i = 0; i < n; i+=offset ) {}

Then you will reduce the time if path have a lot of points.

Yes, I think if I play with the offset I might be able to get a value that given the perfect balance between accuracy and computation. Thanks

TBH this is going to require some Calculus. I recall a similar problem from Differential Calculus, and I think the teach was just previewing Integral Calculus for us. But its definitely a calculus problem. The cool part about using Calculus for this (as opposed to some witchcraft) is you can get damn accurate values.

In Calculus, the concept youre talking about is called Optimization. When you optimize in Calculus, you're finding the closest point on a graph. This method is useful if you're using a curved path that uses a graph/function instead of a set of points.

I suggest you check out some videos on this concept, here's a video: https://www.youtube.com/watch?v=QEF6RteZ4FU

**Answer** by Bunny83
·
Jun 12, 2016 at 10:08 PM

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.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.