TExcelTAD
Home › Game AI › Sense, Think, Act

Sense, Think, Act

Almost every game AI, from a Pac-Man ghost to a squad of soldiers, is built from the same three stages that run again and again:

text
   ┌──────────┐      facts       ┌──────────┐     intent      ┌──────────┐
   │  SENSE   │ ───────────────► │  THINK   │ ──────────────► │   ACT    │
   │ see, hear│   (world model)  │ decide   │ (goal, target)  │ move,    │
   │ remember │                  │ what now │                 │ animate  │
   └──────────┘                  └──────────┘                 └──────────┘
        ▲                                                           │
        └──────────────────── the world changes ◄───────────────────┘
  • Sense turns the raw world into facts the agent believes: "I can see the player at (4, 2)", "I heard a noise near the door 3 seconds ago", "my health is 30%". The agent doesn't read the player's position directly. It reads what its senses report, which may be late, partial or wrong. That is what makes stealth games work.
  • Think chooses what to do from those facts: a state machine, behavior tree, utility scores or a planner. The result is an intent: "go to (4, 2)", "attack the player", "take cover behind crate B".
  • Act carries the intent out: pathfinding, steering, animation, firing a weapon. Acting changes the world, which the senses pick up next tick.

Keeping the three stages separate is the most useful structural decision in AI code. You can swap a state machine for a behavior tree without touching movement, test decisions with fake perception data, and debug by printing what each stage produced.

The three stages in code

The model below keeps the stages as separate functions that pass plain data along. The guard's perception has a range of 6 units, and it remembers the last place it saw the player. Watch what happens when the player steps out of range: the guard doesn't know where the player is, only where they were.

csharp · try it yourself
using System;

var guard = new Guard { X = 0 };
double[] playerPath = { 9, 7, 5, 9, 14, 14, 14, 14 };

for (int tick = 0; tick < playerPath.Length; tick++)
{
    Percept p = Sense(guard, playerPath[tick]);
    Intent i = Think(guard, p);
    Act(guard, i);
    Console.WriteLine(
quot;tick {tick}: player {playerPath[tick],4:F1} | sees {(p.SeesPlayer ? "yes" : "no "),-3} | " +
quot;{i.Kind,-11} target {(i.Target is double t ? t.ToString("F1") : "-"),-4} | guard {guard.X:F1}"); } static Percept Sense(Guard g, double playerX) { bool sees = Math.Abs(playerX - g.X) <= 6; if (sees) g.LastSeen = playerX; // memory survives losing sight return new Percept(sees, g.LastSeen); } static Intent Think(Guard g, Percept p) { if (p.SeesPlayer) return new Intent("Chase", p.LastSeen); if (p.LastSeen is double spot && Math.Abs(spot - g.X) > 0.01) return new Intent("Investigate", spot); return new Intent("Idle", null); } static void Act(Guard g, Intent i) { if (i.Target is not double target) return; double step = Math.Clamp(target - g.X, -1.5, 1.5); // max speed 1.5 per tick g.X += step; } class Guard { public double X; public double? LastSeen; } record Percept(bool SeesPlayer, double? LastSeen); record Intent(string Kind, double? Target);
tick 0: player  9.0 | sees no  | Idle        target -    | guard 0.0
tick 1: player  7.0 | sees no  | Idle        target -    | guard 0.0
tick 2: player  5.0 | sees yes | Chase       target 5.0  | guard 1.5
tick 3: player  9.0 | sees no  | Investigate target 5.0  | guard 3.0
tick 4: player 14.0 | sees no  | Investigate target 5.0  | guard 4.5
tick 5: player 14.0 | sees no  | Investigate target 5.0  | guard 5.0
tick 6: player 14.0 | sees no  | Idle        target -    | guard 5.0
tick 7: player 14.0 | sees no  | Idle        target -    | guard 5.0

At tick 3 the player sprints out of range. The guard can no longer see them, so it walks to the last known position (5.0), finds nothing there by tick 6 and goes idle, while the player is safely at 14. Each stage is tiny and testable on its own, and the log shows exactly which stage to blame when the behaviour looks wrong.

Tick rates: not everything runs every frame

The three stages don't need the same frequency:

Stage Typical rate Why
Act: steering, animation every frame (or every physics step) motion must be smooth
Think: decisions 4–10 times per second humans react in ~200 ms anyway
Sense: line-of-sight rays 2–10 times per second, often round-robin raycasts are expensive
Pathfinding on demand, queued, a few per frame the most expensive query of all

Running decisions less often costs nothing visible and saves a lot of CPU. The trick is to stagger agents so they don't all think on the same frame, which would cause a spike every 10th frame instead of a flat cost:

csharp · try it yourself
using System;

const int interval = 4;               // each agent thinks every 4th frame
string[] agents = { "A", "B", "C", "D", "E", "F", "G", "H" };

foreach (bool staggered in new[] { false, true })
{
    Console.WriteLine(staggered ? "staggered (offset = index % interval):" : "all agents on the same frame:");
    for (int frame = 0; frame < 8; frame++)
    {
        string thinking = "";
        for (int i = 0; i < agents.Length; i++)
        {
            int offset = staggered ? i % interval : 0;
            if ((frame + offset) % interval == 0) thinking += agents[i];
        }
        Console.WriteLine(
quot; frame {frame}: {thinking.Length} thinking {thinking}"); } }
all agents on the same frame:
  frame 0: 8 thinking ABCDEFGH
  frame 1: 0 thinking
  frame 2: 0 thinking
  frame 3: 0 thinking
  frame 4: 8 thinking ABCDEFGH
  frame 5: 0 thinking
  frame 6: 0 thinking
  frame 7: 0 thinking
staggered (offset = index % interval):
  frame 0: 2 thinking AE
  frame 1: 2 thinking DH
  frame 2: 2 thinking CG
  frame 3: 2 thinking BF
  frame 4: 2 thinking AE
  frame 5: 2 thinking DH
  frame 6: 2 thinking CG
  frame 7: 2 thinking BF

Both versions do the same total work, but the staggered one spreads it evenly across frames. The Field Notes: Performance lesson takes this further with per-frame budgets and request queues.

Where the stages live in engines

Stage Unity Unreal Engine 5 Godot 4
Sense your own raycasts and triggers, or a package UAIPerceptionComponent (sight, hearing, damage senses) RayCast3D, Area3D, PhysicsDirectSpaceState3D queries
Think your FSM, or a Behavior graph (Unity Behavior package) Behavior Tree + Blackboard, or StateTree your FSM in GDScript, or a community behavior tree addon
Act NavMeshAgent, Animator AIController move requests, Character Movement, animation NavigationAgent3D, CharacterBody3D, AnimationTree

The engine mapping lessons go through each one.

Note: "Sense" includes memory. A guard who forgets you the instant you duck behind a wall looks broken. Even the simplest perception should remember the last known position and how long ago it was seen; see Memory & Awareness.

Test yourself

Why should the Think stage read percepts instead of the player's real position?

  1. It is faster
  2. So the AI only knows what it could plausibly sense, which makes stealth and fair play possible
  3. Engines forbid reading positions
  4. To save memory

Why stagger agents' decision updates across frames?

  1. To make them dumber
  2. To spread the same total work evenly and avoid periodic frame spikes
  3. Because decisions must run every frame
  4. To use more threads

Which stage usually runs every frame?

  1. Sense
  2. Think
  3. Act (steering and animation)
  4. Pathfinding