public float PointDistanceAlongPath(Vector2 point)
    {
        int currentSegmentIndex = ClosestSegmentIndexToPoint(point);

        float distance = 0;

        for (int i = 1; i < currentSegmentIndex; i++)
        {
            distance += Vector2.Distance(path[i - 1], path[i]);
        }
        Vector2 projected = VectorExtras.ProjectPointOnLineSegment(path[currentSegmentIndex - 1], path[currentSegmentIndex], point);

        distance += Vector2.Distance(path[currentSegmentIndex - 1], projected);

        return(distance);
    }
    public int ClosestSegmentIndexToPoint(Vector2 point)
    {
        float shortestDistance    = float.MaxValue;
        int   currentSegmentIndex = 0;

        for (int i = 1; i < path.Count; i++)
        {
            Vector2 projectedPos = VectorExtras.ProjectPointOnLineSegment(path[i - 1], path[i], point);
            float   dist         = Vector2.Distance(projectedPos, point);
            if (dist < shortestDistance)
            {
                shortestDistance    = dist;
                currentSegmentIndex = i;
            }
        }

        return(currentSegmentIndex);
    }
    Vector2 ClosestPointAlongPath(Vector2 point)
    {
        int currentSegmentIndex = ClosestSegmentIndexToPoint(point);

        return(VectorExtras.ProjectPointOnLineSegment(path[currentSegmentIndex - 1], path[currentSegmentIndex], point));
    }