# 1. Utility AI Concepts and When to Use It
Part of [[Utility AI]]. Next: [[2. Response Curves]].
## 1.1 The idea in one example
A guard can Attack, Reload, Flee, Heal, Investigate or Patrol. Instead of drawing arrows between those states, give each one a score:
- **Attack** = can see the player × healthy enough × has ammo × close enough
- **Flee** = can see the player × badly hurt, × 1.5 because staying alive matters more
- **Patrol** = nothing is going on, × 0.25 so anything else beats it
Each factor is a **consideration**: one fact (health 0.2) passed through one **curve** that says how much that fact matters ("being hurt matters a little at 60 %, a lot at 20 %"). Multiply them, and the agent picks the action with the highest result. With full health the guard attacks (0.94). At 20 % health it flees (0.96 against Attack's 0.31). Nobody wrote a "Low health" transition. The behaviour comes from the curves.
## 1.2 Compared with the alternatives
| Approach | You write | Good at | Weak at |
|---|---|---|---|
| Finite state machine ([[Enemy AI State Machine]]) | states and transitions | few states, exact control, easy to debug | transitions grow with the square of states |
| Behaviour tree / graph ([[Unity Behavior]]) | a priority tree of tasks | sequences ("walk, open door, enter"), designer tools | weighing many soft factors at once |
| Utility AI (this series) | actions and scoring curves | many options, smooth trade-offs, adding actions | long scripted sequences; can be hard to predict |
| GOAP (goal-oriented action planning) | actions with preconditions and effects | multi-step plans to reach a goal | cost, debugging, tuning |
| ML-Agents ([[ML-Agents and Inference Engine]]) | rewards and training | behaviour too complex to hand-author | control, iteration speed |
Use utility when an agent has **many options whose value changes smoothly** with the situation: squad shooters, city-builder citizens, survival-game animals, companions. Use a state machine when there are four states and you need exact, explainable rules.
## 1.3 Utility on top of behaviour graphs
They mix well. Let utility choose **what** to do, and a behaviour graph or a small state machine do **how**. In this series each action's "how" is an `IUtilityBehaviour` component (part 5). If you use [[Unity Behavior]], you could make one behaviour that starts a Behavior Graph agent's subgraph instead. *(Not built or tested here.)*
## 1.4 The five pieces
```text
Sensors ──write──▶ Blackboard (facts: health 0.3, threat_distance 8, ...)
│ read
▼
Considerations (fact ─▶ normalize ─▶ curve ─▶ 0..1)
│ multiply, compensate, × weight
▼
Reasoner (momentum, commitment, cooldowns, pick)
│ chosen action id
▼
Brain ─▶ Behaviour (Enter / Tick / Exit on the NavMeshAgent)
```
1. **Sensors** turn the scene into numbers. They're the only part that touches physics.
2. The **blackboard** holds those numbers.
3. **Considerations and actions** turn numbers into scores. Pure C#, tested in part 6.
4. The **reasoner** chooses. Also pure C#.
5. The **brain** and **behaviours** act on the choice in Unity.
Keeping 3 and 4 free of `UnityEngine` is what makes the system testable and cheap: scoring reads a dictionary, never the scene. [[Game Programming Patterns (Review)]] calls this decoupling; each behaviour is also a Strategy the brain swaps in and out.
## 1.5 What makes utility agents fun
Scores that change smoothly can produce indecisive agents that twitch between two actions. Part 4 fixes that with momentum and commitment. And an agent that always picks the mathematically best move can feel robotic, so the reasoner also offers weighted random choice among close options. [[Level Up! (Review)]]'s rule for enemies applies: the player should be able to read what the enemy is doing and why. Readable beats optimal.
Next: [[2. Response Curves]].