Comments and 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 comments and answers for the question "Finding distance of player to a path in 3D"Comment by phxvyper
http://answers.unity.com/comments/1201809/view.html
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 ins$$anonymous$$d 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=QEF6RteZ4FUMon, 13 Jun 2016 03:05:39 GMTphxvyperAnswer 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 GMTBunny83Comment by shreypareek1991 on shreypareek1991's comment
http://answers.unity.com/comments/1201700/view.html
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.
ThanksSun, 12 Jun 2016 21:14:44 GMTshreypareek1991Comment by TonicMind
http://answers.unity.com/comments/1201657/view.html
TBH this is going to require some Calculus. I recall a similar problem from Differential Calculus, and I think the $$anonymous$$ch 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.Sun, 12 Jun 2016 18:43:41 GMTTonicMindComment by Masterio
http://answers.unity.com/comments/1201622/view.html
$$anonymous$$aybe 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.Sun, 12 Jun 2016 17:28:07 GMTMasterio