Answers for "Finding distance of player to a path in 3D"
http://answers.unity.com/questions/1201367/finding-distance-of-player-to-a-path-in-3d.html
The latest answers for the question "Finding distance of player to a path in 3D"Answer by Bunny83
http://answers.unity.com/answers/1201713/view.html
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.Sun, 12 Jun 2016 22:08:32 GMTBunny83