Get A point partway along a NavMeshAgents path.

Hi,

I’m looking for a way to find out what point is 2 meters along a navmesh agents path. I’m trying to skip the agent ahead on its path a set distance. the NavMeshAgent.path variable in the Unity docs is pretty bare, but I think that’s where the information I need is. Any tips on how to calculate this?

Dont Worry guys, I solved it :wink:

Vector3 FindPointAlongPath(Vector3[] path, float distanceToTravel)
    {
        if(distanceToTravel < 0)
        {
            return path[0];
        }

        //Loop Through Each Corner in Path
        for (int i = 0; i < path.Length -1; i++)
        {
            //If the distance between the next to points is less than the distance you have left to travel
            if (distanceToTravel <= Vector3.Distance(path[i], path[i + 1]))
            {
                //Calculate the point that is the correct distance between the two points and return it
                Vector3 directionToTravel = path[i + 1] - path[i];
                directionToTravel.Normalize();
                return (path[i] + (directionToTravel * distanceToTravel));
            }
            else
            {
                //otherwise subtract the distance between those 2 points from the distance left to travel
                distanceToTravel -= Vector3.Distance(path[i], path[i + 1]);
            }
        }

        //if the distance to travel is greater than the distance of the path, return the final point
        return path[path.Length - 1];
    }
3 Likes