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.
Heads Up Display and User Interface
I've decided to go with the old fashioned bottom heads up display that most RTS games use. The tricky part about using this with Unity's canvas was the scaling. Using the expand with screen option would work for the heads up display, but the drag select would be thrown off.
So I opted to just keep the canvas at a constant pixel size and wrote my own scaling script that also keeps the aspect ratio of the canvas elements.
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 - Youtube.com
If you would like to follow me on my game dev journey, don't forget to subscribe or follow on social media!
Facebook - Facebook.com
Twitter - Twitter.com
Instagram - Instagram.com
Discord Server - Discord.gg
IndieDB - Indiedb.com