- Home /
Question by
bemed · Mar 26, 2015 at 09:15 PM ·
point-a-to-b
How to determine the closest point on a line to another point
I'm making a game where you can change paths whenever the player wants but I don't know how to make the player change paths like on the image below.
The player also needs to maintain the direction that he is going to.
Thank you for your help.
point-on-a-path.jpg
(28.6 kB)
Comment
Answer by bemed · Mar 26, 2015 at 09:16 PM
I'm using the following code to draw my path:
public class RailPath : MonoBehaviour
{
public PathPoint [] nodes;
[System.Serializable]
public class PathPoint
{
public bool jumpToThisPoint;
public Vector3 point;
}
public int [] path;
void OnDrawGizmos()
{
for (int i = 0; i < nodes.Length; ++i)
{
Gizmos.DrawWireSphere(nodes[i].point, 0.5f);
if (i > 0)
{
Gizmos.DrawLine(nodes[i].point, nodes[i -1].point);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613191347im_/https://answers.unity.com/themes/thub/images/avi.jpg)