I am making a tower defense game using A* algorithm for creeps. The creeps can attack ground units in the game. I have put sphere collider on creeps to detect near by ground units. But I don’t want the creeps to go backwards and attack ground units. Basically they should attack ground units that are in front of them.
So first you would do request a FloodPath from the target, but modify the code for it a bit so that is stops after it has found your creep.
Then you would do another floodPath (from the creep), but this time it will only search the nodes which were also searched in the first floodPath (i.e closer to the target) and it stops when it has found the first unit.
So this would define “in front of” as “A node such that it has a shorter or equal path distance to the goal” (but probably with some margin), and it would find the closest unit such that is was in front of the creep. Do you think this would work?
I think that a flood field from start to end and cache the distance from target for each node. Then, if you want to know if a creeps is forward another, simple get nearest node and check it cached distance if is lower.