RTS Opponent AI
Play against the computer
Enemy Masses RTS AI
The EnemyMassesRTSAI is a server-controlled AI opponent designed for RTS games. It provides autonomous control of non-player factions with intelligent tactical decision-making, including exploration, combat engagement, retreat behavior, and advanced baiting/trapping tactics.

Overview
The RTS AI acts as a computer-controlled opponent that manages its own faction of agents, making decisions based on:
Force ratios – Comparing army sizes to decide when to engage or retreat
Tactical awareness – Using advanced maneuvers like baiting and trapping
AoE threat detection – Automatically spreading formations against area-of-effect attacks
Difficulty scaling – Adjustable intelligence and reaction times
Quick Start
Adding the RTS AI to Your Scene
Create the AI Controller:
GameObject → Enemy Masses → RTS AI Controller
Configure Faction Control:
Set Controlled Factions to the factions the AI should command
Set Enemy Factions or enable Auto Detect Enemy Factions to identify targets
Assign References:
Link the Crowd Controller (auto-detected if not set)
Link the Player RTS Controller for automatic enemy faction detection
AI States
Idle
No active objective
Waits for enemies or transitions to exploration
Exploring
Scouting the map
Moves to random points within exploration radius
Engaging
In combat
Aggressively attacks enemy forces
Retreating
Falling back
Moves away from threats when outnumbered
Regrouping
Consolidating forces
Gathers scattered units before next action
SettingTrap
Positioning ambush
Moving main force to ambush position
Baiting
Luring enemies
Using small bait force to draw enemies into trap
Inspector Settings
References
Crowd Controller
The EnemyMassesCrowdController to command. Auto-detected if null.
Player RTS Controller
Reference to identify player-controlled factions as enemies.
Faction Control
Controlled Factions
Factions that this AI is allowed to control.
Enemy Factions
Factions to attack (used when auto-detect is disabled).
Auto Detect Enemy Factions
Automatically treats player-controlled factions as enemies.
When Auto Detect Enemy Factions is enabled, the AI reads faction data from the referenced EnemyMassesRTSController to determine which factions are hostile.
Difficulty Settings
Difficulty
0-5
AI difficulty level (Beginner → Nightmare). Affects tactical decisions and timing.
Decision Interval
0.1-2s
How often the AI makes decisions. Lower = more responsive but higher CPU.
Decision Variance
0-0.5s
Random variance in decision timing for less predictable behavior.
Difficulty Levels
0
Beginner
Rarely uses tactics, slow reactions
1
Easy
Occasional tactics, moderate reactions
2
Normal
Balanced tactical use, standard reactions
3
Hard
Frequent tactics, faster reactions
4
Expert
Advanced tactics, quick reactions
5
Nightmare
Maximum tactical intelligence, instant reactions
Exploration Settings
Enable Exploration
bool
Enable map exploration when not in combat.
Exploration Radius
10-200
Radius around current position to explore.
Exploration Duration
5-60s
How long to explore an area before moving on.
Min Exploration Distance
5-50
Minimum distance to travel for a new exploration target.
Combat Tactics
Enable Advanced Tactics
bool
Enable baiting, trapping, and flanking behaviors.
Min Agents For Baiting
5-100
Minimum agents required to attempt baiting tactics.
Bait Percentage
1-20%
Percentage of forces to use as bait.
Bait Retreat Distance
10-100
Distance bait units retreat to lure enemies.
Engagement Range
10-100
Engage enemies when within this range.
Retreat Ratio
1.5-5.0
Retreat when outnumbered by this ratio.
Regroup Distance
20-100
Regroup when forces spread beyond this distance.
Retreat Ratio Example: With a ratio of 2.5, the AI will retreat when enemies outnumber it 2.5:1 (e.g., 100 AI agents vs 250+ enemies).
AoE Avoidance (Formation Spacing)
The AI can automatically detect AoE (Area of Effect) threats from enemy factions and spread out formations to minimize damage.
Enable AoE Avoidance
bool
Enable dynamic formation spacing.
Normal Spacing
0.5-3
Formation spacing when no AoE threat detected.
Spread Spacing
2-6
Spread out spacing when AoE threat detected.
Max Spread Spacing
4-10
Maximum spacing against heavy AoE threats.
Spacing Adjustment Speed
0.5-5s
How quickly spacing adjusts to threats.
Auto Detect AoE Threats
bool
Automatically scan enemy EnemyCrowd settings for AoE.
Manual AoE Threat Level
0-1
Manual override when auto-detect is disabled.
AoE Threshold Radius
1-10
Minimum damage radius to consider as AoE threat.
How AoE Detection Works
When Auto Detect AoE Threats is enabled, the AI scans enemy faction EnemyCrowd configurations for:
Projectile AoE – Checks
projectileConfig.enableAOEandaoeRadiusSecondary Projectile AoE – Checks secondary attack configs
Arc Projectiles – Grenades/catapults using
ProjectileArcmode get extra threat weightMultiple AoE Factions – Threat level increases with more AoE-capable enemies
Spawning (Optional)
The AI can spawn reinforcements over time to replenish losses.
Enable Spawning
bool
Enable AI-controlled spawning of reinforcements.
Max Agent Count
10-1000
Maximum total agents the AI can have at once.
Spawn Wave Size
1-100
Agents to spawn per wave.
Spawn Interval
5-300s
Minimum seconds between spawn waves.
Spawn Formation
enum
Formation type for spawned agents.
Spawn Location
Transform
Spawn location. If null, spawns near existing forces.
Visual Feedback & Debug
Show Gizmos
Show AI state gizmos in the editor Scene view.
AI Color
Color for AI controlled agents visualization.
Debug Logs
Enable debug logging for AI decisions in the Console.
Public API
Properties
Manual Control Methods
AoE Avoidance Control
Tactical Behaviors
Baiting Tactic
When the AI has sufficient forces and tactical conditions are favorable:
Selection – AI selects a small percentage of agents as "bait"
Trap Setup – Main force moves to an ambush position behind the army center
Luring – Bait units approach enemies aggressively
Springing – When enemies pursue bait to the trap position, main force engages
Retreat Logic
The AI retreats when significantly outnumbered:
Regrouping
Forces are considered "scattered" when more than 30% of agents are beyond regroupDistance from the army center. The AI will pause offensive actions to consolidate before continuing.
Integration Examples
Difficulty Scaling Based on Player Performance
Scripted AoE Warning
Wave-Based Spawning Controller
Performance Considerations
Decision Interval
Lower = more CPU
Use 0.5s for most games, 0.2s for competitive
Max Agents Per Decision
Batched processing
Internal limit of 100 agents per frame
AoE Detection
1 second cache
Negligible impact
Debug Logs
Console overhead
Disable in production
Gizmo Visualization
When Show Gizmos is enabled, the Scene view displays:
State Sphere
Varies
Current AI state at controller position
Army Center
AI Color (orange)
Center of controlled forces
Exploration Radius
Green
Current exploration area
Trap Position
Red
Ambush location during baiting
State Colors
Idle
Gray
Exploring
Green
Engaging
Red
Retreating
Yellow
Regrouping
Cyan
SettingTrap
Orange
Baiting
Magenta
Troubleshooting
AI Not Controlling Any Agents
Verify Controlled Factions includes your AI faction names
Ensure agents exist in the scene with matching faction names
Check that agents are not dead (
isDead = false)
AI Not Attacking
Check Enemy Factions configuration or enable Auto Detect Enemy Factions
Verify Player RTS Controller reference is set (for auto-detect)
Ensure enemy agents are within Engagement Range
AI Retreats Too Often
Increase Retreat Ratio (higher = less likely to retreat)
Ensure your AI faction has enough spawned agents
Formations Not Spreading for AoE
Enable Enable AoE Avoidance
Check that enemy factions have
enableAOEconfigured in theirProjectileConfigVerify AoE Threshold Radius isn't set too high
Try manually setting threat level with
SetAoEThreatLevel()
Performance Issues with Large Armies
Increase Decision Interval to 1.0s or higher
Reduce Spawn Wave Size
Disable Debug Logs
See Also
Public API Reference – Complete API documentation
RTS World Anchor System – Fog of war and minimap integration
Networking Overview – Multiplayer synchronization
Last updated