Pathfinding on moving targets


I have a project which requires me to dynamically pathfind to moving targets, currently my code looks like this:

void Update(){

void Pathfind(Vector3 target){
    // if the target hasn't moved enough, ignore this request
    if (Distance(target, previousTarget) < 1) return;

    // use seeker to move to the place

But the issue I’m having is that when the object moves, the enemy stops when pathfinding is calculated, so it’s straightforward to outrun them. (Moving from tile to tile causes them to pause for like 0.5s).

I am using a custom GridGraph, game is 2D.


Are you using a custom movement script or a built-in one?
For the built-in movement scripts, just setting the destination property (or using the AIDestinationSetter component) and using the Dynamic mode will work very well for moving targets.

Hi Aron,

Thanks for your reply. I am using custom movement. Essentially I save the Path in my script when pathfinding was successful. Then do this:

Vector3 nextWaypoint = m_Path.vectorPath[m_CurrentWaypoint];
Vector3 newPosition = Vector3.MoveTowards(transform.position, nextWaypoint, movementSpeed * Time.deltaTime);