Buildings & Units
This section describes how buildings and units work together in the template.
Building placement
Handled primarily by BuildModeController + BuildingType.
Typical flow:
- Player clicks a building button in the UI.
- BuildModeController activates placement mode with a specific BuildingType.
- A ghost prefab is spawned and follows the mouse over the ground.
- The placement system:
- Raycasts to the ground
- Snaps to a grid (if enabled)
- Validates placement:
- Is the area free of other buildings?
- Is the ground allowed for this building? (e.g., only on resource deposits)
- When the player confirms (e.g. left click):
- Resources are checked via ResourceManager.TrySpend
- A construction or final building prefab is instantiated
The ghost mesh is usually tinted green for valid placement and red for invalid.
Building
Building usually derives from Damageable and represents a placed building instance.
Common responsibilities:
- Store a reference to its BuildingType
- Track construction state (if you implement build times):
- Is the building completed or still under construction?
- Optionally hold worker assignment / production components
Buildings can host:
- UnitProducer (for unit production)
- ResourceProducer (for passive or worker-driven resource generation)
- EnemySpawnerBuilding (for AI bases)
Unit production (UnitProducer)
UnitProducer is typically attached to a building that can create units (e.g. Town Center, Barracks).
Responsibilities:
- Show available unit types in the building UI
- Trigger production:
- Check resources via ResourceManager
- Check population capacity
- Spawn units at an output position (spawn point near the building)
- Notify the game manager / resource manager about population changes
Typical flow:
- Player selects the building.
- Building UI displays unit buttons based on the UnitProducer configuration.
- Player clicks a unit button.
- UnitProducer:
- Checks cost and population
- Deducts required resources
- Optionally starts a production timer
- When production finishes:
- Instantiate the unit prefab
- Register the unit in RTSGameManager
- Increase population usage
Unit
Unit also derives from Damageable and usually contains:
- A NavMeshAgent for movement
- A reference to UnitType
- Helpers:
- MoveTo(Vector3 destination, float stoppingDistance = 0f)
- Animation hooks (if needed)
Movement is requested by:
- UnitSelectionController (player issuing right-click commands)
- AI scripts (EnemyUnitAI)
Unit itself does not contain combat logic; that is separated into Attacker.
Worker assignment & resource production
For resource buildings you typically have:
- A ResourceProducer component
- A worker assignment system (list of Units currently working here)
Typical pattern:
- Building has a max workers count (e.g. 3).
- Player selects the building and uses UI to assign available workers.
- Assigned workers:
- Receive a MoveTo command towards the worksite.
- Once they reach it, the building counts them as active workers.
- ResourceProducer uses the number of active workers to determine production rate.
- Produced resources are added to ResourceManager, respecting storage limits.