A* Pathfinding Project

Path towards the nearest target with node penalties


Hey there,

I tried searching for my specific use case but couldn’t find an answer (at least my search terms were not providing hits).

I’m developing a turn-based game with grid-based movement. Units can move N number of spaces and movement is affected by node penalties.

For my AI, I’ve been trying to get a unit to move towards a selected target each time it’s the AI’s turn to move. However, since the target is out of range (i.e the penalties prevent a path from being found) the unit will obviously not move anywhere.

I’m trying to figure out if there is a way to tell the system to give me a valid path within my movement range that’ll lead me towards the target - that way each turn the unit will move ever closer to the target until it’s right up next to it.

Targets are always reachable, even if the AI unit can only move a maximum of 1 space a turn, I want to know which space it should be. Is this possible with the API?

I’ve tried using XPath.Construct with an instance of EndingConditionDistance

EndingConditionDistance ecd = new EndingConditionDistance(path, 999999);

However, it doesn’t seem to work either. I’ve specifically put a breakpoint in the body of the TargetFound function and it’s never hit.


So I’ve noticed that if penalties are set to 0, then the TargetFound method is called and evaluated.
If penalties are > 0 then TargetFound is never called. I believe this may be a bug?

Same thing happens with ABPathEndingCondition



I would recommend that you calculate the full path to the target, but then only move one step along it.
Something like

var path = Seeker.StartPath(transform.position, target.position);
// Synchronous path request for simplicity
if (path.path.Count > 1) {
    var nodeToMoveTo = path.path[1];
    var positionToMoveTo = (Vector3)nodeToMoveTo.position;