Clipping the end of the path?

Is there an easy way to just cut off the last X number of steps in a path? Iā€™d like make a path from enemy to player but stop X feet away and engage in attack behavior.

Or chase the player but never get closer than X units and what not.

I figure making a path to the player and editing the vectorpath (which seems awfully tedious) might be the only way I see and was hoping someone knew better than I. :slight_smile:



That will however not guarantee that the player is not close enough, but on the other side of a wall. To guarantee that, you will have to edit the vectorPath, it is not that tedious.
Something like this would work.
List<Vector3> vectorPath = new List<Vector3> (); float d = 0; if (limit <= 0) vectorPath.Add (path.vectorPath[path.vectorPath.Length-1]); for (int i=path.vectorPath.Length-2;i>=0;i--) { d += Vector3.Distance (path.vectorPath[i+1],path.vectorPath[i]); if (d >= limit) vectorPath.Add (path.vectorPath[i]); } vectorPath.Reverse ();
As a bonus, you get the total length of the path!

The above code can be optimized, but it should be fast enough for common use I think.
The above code is not tested either, I hope it works.

Thanks :slight_smile: