A* Pathfinding Project

[Solved] Calculate a path and then move the agent to a specific point in the path


#1

Some context on what I’m building:
I’m using the pathfinding for NPC agents that have a schedule. When the player enters a scene, the NPC will have a start point and a target point. For example, at 10:00 in-game time, the NPC should be at the station. He starts walking to the station from his house at 9:00. So every time at 9:00, this NPC will calculate a path to the target point and start walking there. Because there are dynamic obstacles, I can’t re-use one path and have to calculate one every time. This process is working perfect with this package.

But now I have a challenge. What if the player enters the scene at 9:30 in-game time?

My own solution (not implemented yet) is a bit messy:
I check that the NPC had a total 60 minute travel time from start to end and he has 30 minutes left, so he should be 50% of the way to his target. So I take the end point and calculate a random path to a point thats roughly 50% of the distance between start/end based on movespeed, remaining time and total distance from start to end. Then from this random point that’s roughly 50% away of the target, I can calculate a new path to the target. But my problem is that the random point at 50% of the distance is never accurate.

What would be perfect, but I don’t know if it’s possible:
When the player enters the scene at 9:30. The NPC agent will know it’s start position and end position. The “normal” path will be calculated from start to end. Then we know he should actually be at 50% of the way again. But this time, we use the original start -> end path and we move the agent to a waypoint that’s 50% of the path length. But I don’t think there is a way to determine the length of a path or put a point on e.g. 50% of 70% of the way in the path.

I hope I gave enough context for my problem. Would appreciate any feedback if you guys have had the same issue before or know of some potential solutions.


#2

Hi

If you have the path this can easily be calculated.

ABPath path = ...;
// Create a PathInterpolator helper object
PathInterpolator interpolator = new PathInterpolator();
interpolator.SetPath(path.vectorPath);
// Set the travelled distance of the path interpolator to be 50% of the remaining distance.
// Since the interpolator starts at the beginning of the path this will be 50% of the total path length
interpolator.distance = interpolator.remainingDistance * 0.5f;
// Move the transform to the point given by the interpolator
transform.position = interpolation.position;

#3

Hi Aron,

This is great. I wasn’t aware of the PathInterpolator class, but I’m sure this will solve my problem. I will try this asap, thanks for helping me here! :smile: