Home

Published

- 1 min read

unity find closest point on line

img of unity find closest point on line

The solution for this is noted below

unity find closest point on line

Solution

   public Vector2 FindNearestPointOnLine(Vector2 origin, Vector2 direction, Vector2 point)
{
    direction.Normalize();
    Vector2 lhs = point - origin;

    float dotP = Vector2.Dot(lhs, direction);
    return origin + direction * dotP;
}

Try other methods by searching on the site. That is if this doesn’t work