public static Vec3 ProjectPosOnPath(Vec3 pos, List <Vec3> path, out int projection_segment_start_idx, out float scalar_projection) { Vec3 projection = path[0]; projection_segment_start_idx = 0; scalar_projection = 0; float dist = pos.Distance2D(projection); // find closest pos projection on path for (int i = 0; i < path.Count - 1; ++i) { var p = Vec3.ProjectPointOnSegment(path[i], path[i + 1], pos, out var sp, true); var d = pos.Distance2D(p); if (d < dist) { projection = p; dist = d; projection_segment_start_idx = i; scalar_projection = sp; } } return(projection); }