RTS Style Game - Handling units, ideas?

Well hello good people,
I’m in need of ideas right now =)

I’m creating RTS game, and it’s been going nicely, but now there is a big idea (and a big mechanism behind it) to realize…

I have created a tank prefab, it has its own code, it checks if mouse has selected it, can move around, etc… It’s all encapsulated…

And I have a building, which should serve as factory, should be able to build tanks, which are prefabs themselves…

Now the big question, how should I handle creating tanks from the factory? And please don’t flame me with answers “Don’t expect someone to write a game for you” etc, because I’m not looking for code actually, I’m looking for a good idea, a good way to handle this scenario, since unity has pretty limited content in terms of tutorials for RTS genre…

So, how should I go about doing it? Should there, in the background of everything be laying a big List<> with all the tanks currently on the map, and the factory should add it to the list when spawning a new one? Should I have some sort of naming convention, so if a player builds 3 tanks in a row, they’d be as following:

player_tank_t31_01, player_tank_t31_02, player_tank_t31_03..

whistl if enemy build a tank, they’d be named enemy_tank_t31_0? That way, just by reading the tank object’s name we can see a lot of info, like which side is it from, the type of tank, and etc…

I’m just looking for creative ideas, any piece of code is surely welcome, but not required… Come on guys, let’s all have fun and share some ideas? :slight_smile:


alt text

EDIT: For some odd reason, the formatting of my text and the image is not displayed correctly, and can see that this could be a pain to read… :confused: If any moderator could give it a quick edit and see what’s the problem with formatting would be really good :slight_smile:

I don’t know how good or detailed this tutorial is, but the knowledge shared should be invaluable if you are new to the game logic of a RTS.


I made a game that had aspects that operated similarly to RTS AI… I had an AI commander style script (one per faction) which kept track of all units in the faction. This was mainly used for allowing the AI to form a basic plan (assigning units to tasks)
Once a unit spawned, it is added to the enemy commander master list. (The enemy commander script value is also assigned to the spawned unit so they can pass information back to the ‘commander’) All of this is done by the spawn script (instantiate to a variable then add variable to master list, send message to variable to assign commander script )

Enemy detection was done easily; each unit in the game inherited from a master class which had a faction variable (just an int). They also have a detection range, once every second or so the unit runs a detect function which uses overlap sphere, then sorts units into friendly and enemy by comparing faction values (I had a lot of units so lowered the check frequency for performance)
From here, dependant on unit type you can send different information to the ai commander who will then use the list of own units to assign them to react.

So as an example for your game:
Empty game object for enemy commander : add enemy commander ai script to it (which contains list of units, methods to recieve information, assign tasks/waypoints etc) note you can add this to a building or specific unit instead to allow a player to target the command node… If it is killed/destroyed, Individual unit AI will (should!) cover enough to allow them to react to local threats, it just means there will be no coordinated movement.

Add a spawn script to the building which has the enemy commander as a variable, array of the possible spawn prefabs etc. the spawn method will take the unit to spawn, spawn the unit, add the unit to the enemy commander and send message to the spawned unit to assign the ai commander
The unit prefab will have a script inherited from basicGenericUnitScript which has all the unit specific variables and an ai commander script type variable, a function to assign the ai commander script (to receive the spawned locations send message), maybe functions to send information back to ai commander, detection functions, basic combat and movement etc
Note : use inheritance so you when dealing with units you can use get component basicGenericUnitScript on a unit and it will return the specific script : this allows you to make completely different individual unit scripts/behaviours as long as they keep the same function names!

Hope it helps! I will caveat the above : it is all stuff I worked out to solve my specific problems, use at your own risk!!!
(If you want script/code examples let me know!)