A* pathfinding with more than one target

I was following this => (

) tutorial series on A* pathfinding, and I found that it works almost perfectly if there is one target that is used for calculating of paths. I don’t understand what am I supposed to do to make the algorithm effectively calculate more than one path as in to calculate path for each unit if each unit has separate target. I would appreciate if someone could at least explain to me why the algorithm freaks out if i try to use more than one target when doing calculations at the same time.

You don’t need to modify the algorithm but run it several times, with a new target each time.

2 Likes

each unit will need it’s own calculation of a path, from its position to its target

Also just to add since AStar algorithm does not change anything but returns list of nodes that make a path you can possibly run it each in separate thread if you wish.

to that end the same youtuber did something along those lines in the “make a game” playlist for the enemies, worth a look too.

Well if I understand this correctly PathRequestManager.RequestPath(transform.position, target.position, OnPathFound); is what does the calculation of the path

but than, I have two units each has its own target so I did this

PathRequestManager.RequestPath(PlayerSoldier1.transform.position, PlayerSoldier1Target.transform.position, OnPathFound);

PathRequestManager.RequestPath(PlayerSoldier2.transform.position, PlayerSoldier2Target.transform.position, OnPathFound);

but that that makes units enter near infinite loop and also one unit two units loops through both targets.

How should I called this so its not clashing with other units and targets?

look at the “Unit” class in that vid, it handles all the work for you, you just need the two units to have different targets set…

Ok. I see, if there are 2 path request calls the first one gets overridden by the second one so both units go to second target. Both units have separate targets but this still causes the issue to occur.

I will take shot in the dark and say that it is probably static function accessing some static or shared objects

You can see in the video that that its public static void RequestPath and I have more more than one call for PathRequestManager.RequestPath. Was the function supposed to be non static? If its not static I get an error saying ‘an object reference is required to access a non-static member’ which i don’t know how to fix.

… again… you don’t need to do anything with the scripts provided in the vid to make each thing go to it’s target, you only need to set the target in the unit script…

ok, i see what the problem was. I have one more issue, and that is that the script that does all the work needs to be unchecked in inspector for it to work without visible problems. If its checked the unit will ‘jerk’ rotate in place and stick around each waypoint and not continuously move from one waypoint to another like it does if the script is unchecked

How do you change the target with code?