Skip to content

Buildings & Units

This section describes how buildings and units work together in the template.


Building placement

Handled primarily by BuildModeController + BuildingType.

Typical flow:

  1. Player clicks a building button in the UI.
  2. BuildModeController activates placement mode with a specific BuildingType.
  3. A ghost prefab is spawned and follows the mouse over the ground.
  4. The placement system:
  5. Raycasts to the ground
  6. Snaps to a grid (if enabled)
  7. Validates placement:
    • Is the area free of other buildings?
    • Is the ground allowed for this building? (e.g., only on resource deposits)
  8. When the player confirms (e.g. left click):
  9. Resources are checked via ResourceManager.TrySpend
  10. 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:

  1. Player selects the building.
  2. Building UI displays unit buttons based on the UnitProducer configuration.
  3. Player clicks a unit button.
  4. UnitProducer:
  5. Checks cost and population
  6. Deducts required resources
  7. Optionally starts a production timer
  8. When production finishes:
  9. Instantiate the unit prefab
  10. Register the unit in RTSGameManager
  11. 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:

  1. Building has a max workers count (e.g. 3).
  2. Player selects the building and uses UI to assign available workers.
  3. Assigned workers:
  4. Receive a MoveTo command towards the worksite.
  5. Once they reach it, the building counts them as active workers.
  6. ResourceProducer uses the number of active workers to determine production rate.
  7. Produced resources are added to ResourceManager, respecting storage limits.