Sponsored By

The sixth weekly devlog of Deterrence. This week I delve into handling enemy units, scaling heads up display for all resolutions, and implementing A* pathfinding in Unity.

Matthew Gianfrancesco, Blogger

April 26, 2022

2 Min Read

A tower defense mixed with real-time strategy where you repel attacking sentient AI robots until they give up.


Handling Enemy Units

The easiest and most optimal way in my opinion for player units to check for enemy units in their surroundings is for player units to have a static list of enemy units to check distances with. When an enemy is created, it will add itself to the list and vice versa. If the enemy is in range for a unit, then the enemy is added to the units local 'enemies in range' list. Further layers will be added to prioritize what enemies to attack first.

Player unit movement will take in an additonal variable storing the command given by the player. With attack move, the unit should pause it's path to engage enemies in range before finishing the path. And with a regular move command, the unit should finish it's path before engaging any enemies in range.

A* Pathfinding

The A* pathfinding tutorial by Sebastian Lague can be found on YouTube. There were a few modifications I've made to the script to fix some issues I've found with A* pathfinding. I noticed how the A* pathfinding would fail if the target node was an unwalkable node, so I made a quick method to search for a nearby walkable node to find a path to. This way if a player commands units to move towards an unwalkable cliff, the units will still move to the general area.

Another problem with the A* pathfinding is islands. If the start or end points are surrounded by unwalkable nodes, then the path will fail. Determining if either one is on an island using a flood fill method can be expensive on the CPU depending on how large the island is.

One can simply try to solve this problem by finding islands when the grid is created at the start of the game, but that still won't find islands created by the player building walls. If anyone has any solutions, let me know in the comments below!

Sebastian Lague A* Pathfinding Tutorial - https://www.youtube.com/watch?v=-L-WgKMFuhE

Read more about:

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like