Seek, Flee & Arrive
Decision-making picks what a character wants ("go to the alarm", "get away from the grenade"). Steering turns that wish into motion that looks physical: the character speeds up, turns in a curve and slows down, instead of snapping to a new velocity. The idea comes from Craig Reynolds' 1999 paper Steering Behaviors for Autonomous Characters, and it is still how most games move crowds, birds, fish, drones and anything else that isn't following an animation exactly.
The vehicle model
Every steering behavior works on the same small "vehicle":
| Field | Meaning |
|---|---|
Pos |
position |
Vel |
velocity (direction × speed) |
MaxSpeed |
top speed |
MaxForce |
the largest change of velocity per second (its "grip"; physically a force divided by mass) |
A behavior returns a steering force: the change it would like to make to the velocity. The vehicle turns it into an acceleration (here it tries to close the gap in a quarter of a second), clamps that to MaxForce, adds it to the velocity, clamps the speed to MaxSpeed, and moves. Because the change per step is limited, the path always curves smoothly.
The core formula of almost every behavior is:
steering = desired_velocity - current_velocitySeek and flee
Seek wants to move at full speed straight at the target: desired = normalize(target - pos) * MaxSpeed. Flee is the same vector reversed. The program below runs both from the same start, one step every 0.1 s, and prints the position every five steps:
using System;
var target = new V2(10, 0);
var seeker = new Vehicle(new V2(0, 4), new V2(0, -1));
var fleer = new Vehicle(new V2(6, 1), new V2(0, 0));
Console.WriteLine("step seeker fleer");
for (int step = 0; step <= 30; step++)
{
if (step % 5 == 0) Console.WriteLine(quot;{step,4} {seeker.Pos,-14} {fleer.Pos}");
seeker.Apply(Steer.Seek(seeker, target), 0.1);
fleer.Apply(Steer.Flee(fleer, target), 0.1);
}
static class Steer
{
public static V2 Seek(Vehicle v, V2 target) => (target - v.Pos).Norm() * v.MaxSpeed - v.Vel;
public static V2 Flee(Vehicle v, V2 threat) => (v.Pos - threat).Norm() * v.MaxSpeed - v.Vel;
}
class Vehicle(V2 pos, V2 vel)
{
public V2 Pos = pos, Vel = vel;
public double MaxSpeed = 3, MaxForce = 6;
public void Apply(V2 force, double dt)
{
V2 accel = (force * 4).Clamp(MaxForce); // try to close the gap in 0.25 s, within the grip limit
Vel = (Vel + accel * dt).Clamp(MaxSpeed);
Pos += Vel * dt;
}
}
readonly record struct V2(double X, double Y)
{
public static V2 operator +(V2 a, V2 b) => new(a.X + b.X, a.Y + b.Y);
public static V2 operator -(V2 a, V2 b) => new(a.X - b.X, a.Y - b.Y);
public static V2 operator *(V2 a, double k) => new(a.X * k, a.Y * k);
public double Len => Math.Sqrt(X * X + Y * Y);
public V2 Norm() => Len > 1e-9 ? this * (1 / Len) : new V2(0, 0);
public V2 Clamp(double max) => Len > max ? Norm() * max : this;
static string F(double v) => (Math.Round(v, 2) + 0.0).ToString("F2");
public override string ToString() => quot;({F(X)}, {F(Y)})";
}step seeker fleer
0 (0.00, 4.00) (6.00, 1.00)
5 (0.82, 3.47) (5.18, 1.20)
10 (2.18, 2.94) (3.78, 1.55)
15 (3.58, 2.42) (2.33, 1.92)
20 (4.98, 1.89) (0.88, 2.28)
25 (6.38, 1.36) (-0.58, 2.64)
30 (7.79, 0.83) (-2.03, 3.01)
The seeker starts moving down, so it curves round toward the target instead of turning on the spot. The fleer accelerates away along the line from the threat. Notice that seek never stops: it reaches the target at full speed, overshoots, turns and comes back, orbiting forever. That's fine for a homing missile and wrong for a guard walking to a door.
Arrive
Arrive is seek with brakes. Inside a slowing radius the desired speed shrinks in proportion to the distance, so the vehicle glides to a stop on the target:
using System;
var target = new V2(10, 0);
var seek = new Vehicle(new V2(0, 0));
var arrive = new Vehicle(new V2(0, 0));
Console.WriteLine("step seek x arrive x arrive speed");
for (int step = 0; step <= 80; step++)
{
if (step % 8 == 0)
Console.WriteLine(quot;{step,4} {seek.Pos.X,6:F2} {arrive.Pos.X,8:F2} {arrive.Vel.Len,12:F2}");
seek.Apply((target - seek.Pos).Norm() * seek.MaxSpeed - seek.Vel, 0.1);
arrive.Apply(Arrive(arrive, target, slowRadius: 3), 0.1);
}
static V2 Arrive(Vehicle v, V2 target, double slowRadius)
{
V2 toTarget = target - v.Pos;
double dist = toTarget.Len;
if (dist < 0.01) return v.Vel * -10; // close enough: brake hard
double speed = dist < slowRadius ? v.MaxSpeed * dist / slowRadius : v.MaxSpeed;
return toTarget * (speed / dist) - v.Vel;
}
class Vehicle(V2 pos)
{
public V2 Pos = pos, Vel;
public double MaxSpeed = 3, MaxForce = 6;
public void Apply(V2 force, double dt)
{
V2 accel = (force * 4).Clamp(MaxForce); // try to close the gap in 0.25 s, within the grip limit
Vel = (Vel + accel * dt).Clamp(MaxSpeed);
Pos += Vel * dt;
}
}
readonly record struct V2(double X, double Y)
{
public static V2 operator +(V2 a, V2 b) => new(a.X + b.X, a.Y + b.Y);
public static V2 operator -(V2 a, V2 b) => new(a.X - b.X, a.Y - b.Y);
public static V2 operator *(V2 a, double k) => new(a.X * k, a.Y * k);
public double Len => Math.Sqrt(X * X + Y * Y);
public V2 Norm() => Len > 1e-9 ? this * (1 / Len) : new V2(0, 0);
public V2 Clamp(double max) => Len > max ? Norm() * max : this;
}step seek x arrive x arrive speed
0 0.00 0.00 0.00
8 1.69 1.69 2.91
16 4.08 4.08 3.00
24 6.48 6.48 3.00
32 8.88 8.58 1.96
40 10.68 9.51 0.71
48 9.50 9.84 0.24
56 10.38 9.95 0.08
64 9.79 9.98 0.03
72 9.92 9.98 0.01
80 10.25 9.99 0.02
Seek blows past x = 10 and swings back and forth around it; arrive slows down inside the last three units and settles on the target.
Tip: Pick the slowing radius from the vehicle's braking ability: a vehicle at
MaxSpeedneeds aboutMaxSpeed² / (2 · MaxForce)units to stop. With 3 and 6 that's 0.75 units, so a radius of 3 gives a gentle, visible slowdown.
Try it: steer toward the pointer
Click the frame, move the pointer, and press 1 (seek), 2 (flee) or 3 (arrive). The green line is the current velocity, the orange line the steering force. Try raising MaxForce to see how "grip" changes the curves.
<style>
html, body { margin: 0; height: 100%; background: #0e1018; overflow: hidden; }
canvas { display: block; margin: 0 auto; width: min(100vw, 177.78vh); height: auto; outline: none; touch-action: none; }
</style>
<canvas width="480" height="270"></canvas>
<script>
// ---- play kit: click to focus, keep game keys inside the frame, fixed-step loop ----
const cv = document.querySelector('canvas'), ctx = cv.getContext('2d');
const W = cv.width, H = cv.height;
const keys = new Set(), pressed = new Set(); // keys held now / pressed since the last update
let active = false, idleDrawn = false;
cv.tabIndex = 0;
cv.addEventListener('pointerdown', () => cv.focus());
cv.addEventListener('focus', () => { active = true; });
cv.addEventListener('blur', () => { active = false; idleDrawn = false; keys.clear(); pressed.clear(); });
addEventListener('keydown', (e) => {
if (e.code.startsWith('Arrow') || e.code === 'Space') e.preventDefault(); // don't scroll the lesson
if (!e.repeat) pressed.add(e.code);
keys.add(e.code);
});
addEventListener('keyup', (e) => keys.delete(e.code));
function banner(title, sub = '') {
ctx.save();
ctx.fillStyle = 'rgba(0,0,0,0.6)'; ctx.fillRect(0, 0, W, H);
ctx.fillStyle = '#fff'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.font = 'bold 22px system-ui, sans-serif'; ctx.fillText(title, W / 2, H / 2 - 10);
ctx.font = '14px system-ui, sans-serif'; ctx.fillText(sub, W / 2, H / 2 + 18);
ctx.restore();
}
function run(update, draw, help = '') {
const STEP = 1 / 60;
let acc = 0, last = performance.now();
requestAnimationFrame(function frame(now) {
const dt = Math.min((now - last) / 1000, 0.1);
last = now;
if (active) {
acc += dt;
while (acc >= STEP) { update(STEP); pressed.clear(); acc -= STEP; }
draw();
} else if (!idleDrawn) {
draw(); banner('Click to play', help); idleDrawn = true;
}
requestAnimationFrame(frame);
});
}
// ---- end of play kit ----
const MAX_SPEED = 160, MAX_FORCE = 320, SLOW_RADIUS = 90;
const v = { x: 80, y: 135, vx: 0, vy: 60 };
const target = { x: 360, y: 135 };
let mode = 'seek', force = { x: 0, y: 0 };
cv.addEventListener('pointermove', (e) => {
const r = cv.getBoundingClientRect();
target.x = (e.clientX - r.left) * (W / r.width);
target.y = (e.clientY - r.top) * (H / r.height);
});
const len = (x, y) => Math.hypot(x, y);
function clamp(x, y, max) { const l = len(x, y); return l > max ? { x: x / l * max, y: y / l * max } : { x, y }; }
function steering() {
let dx = target.x - v.x, dy = target.y - v.y;
const d = len(dx, dy) || 1;
if (mode === 'flee') { dx = -dx; dy = -dy; }
const speed = mode === 'arrive' && d < SLOW_RADIUS ? MAX_SPEED * d / SLOW_RADIUS : MAX_SPEED;
return { x: dx / d * speed - v.vx, y: dy / d * speed - v.vy };
}
function update(dt) {
if (pressed.has('Digit1')) mode = 'seek';
if (pressed.has('Digit2')) mode = 'flee';
if (pressed.has('Digit3')) mode = 'arrive';
const s = steering();
force = clamp(s.x * 4, s.y * 4, MAX_FORCE); // same model as the C# Vehicle
const nv = clamp(v.vx + force.x * dt, v.vy + force.y * dt, MAX_SPEED);
v.vx = nv.x; v.vy = nv.y;
v.x = (v.x + v.vx * dt + W) % W; // wrap around the edges so flee stays visible
v.y = (v.y + v.vy * dt + H) % H;
}
function draw() {
ctx.fillStyle = '#0e1018'; ctx.fillRect(0, 0, W, H);
if (mode === 'arrive') {
ctx.strokeStyle = '#334'; ctx.beginPath(); ctx.arc(target.x, target.y, SLOW_RADIUS, 0, Math.PI * 2); ctx.stroke();
}
ctx.fillStyle = '#e74c3c'; ctx.beginPath(); ctx.arc(target.x, target.y, 5, 0, Math.PI * 2); ctx.fill();
const a = Math.atan2(v.vy, v.vx);
ctx.save(); ctx.translate(v.x, v.y); ctx.rotate(a);
ctx.fillStyle = '#4fc3f7'; ctx.beginPath(); ctx.moveTo(10, 0); ctx.lineTo(-7, 6); ctx.lineTo(-7, -6); ctx.closePath(); ctx.fill();
ctx.restore();
ctx.lineWidth = 2;
ctx.strokeStyle = '#2ecc71'; ctx.beginPath(); ctx.moveTo(v.x, v.y); ctx.lineTo(v.x + v.vx * 0.3, v.y + v.vy * 0.3); ctx.stroke();
ctx.strokeStyle = '#f39c12'; ctx.beginPath(); ctx.moveTo(v.x, v.y); ctx.lineTo(v.x + force.x * 0.12, v.y + force.y * 0.12); ctx.stroke();
ctx.lineWidth = 1;
ctx.fillStyle = '#ccc'; ctx.font = '13px system-ui, sans-serif';
ctx.fillText(`mode: ${mode} speed: ${len(v.vx, v.vy).toFixed(0)} (1 seek, 2 flee, 3 arrive)`, 10, 18);
}
run(update, draw, 'Move the pointer; keys 1, 2, 3 switch behavior');
</script>Why forces and not velocities?
Setting Vel = desired directly would make the character turn instantly, which looks robotic and ignores momentum. Limiting the change per step gives you:
- Turning circles for free: a fast vehicle with low
MaxForcetakes wide curves, a nimble one turns tightly. - Blending: forces from several behaviors can be added (seek the goal + avoid walls + keep apart from friends), as the next lessons show.
- Tuning knobs designers understand: "heavier" means lower
MaxForce.
In an engine you usually don't integrate positions yourself. You feed the result to a CharacterController/CharacterBody/CharacterMovementComponent as a desired velocity, or to the navigation agent (see Unity: AI Navigation & Behavior and Godot: NavigationAgent3D). The vector math is the same one covered in Math for Games.
Test yourself
What does a basic steering behavior return?
- A new position
- A steering force: desired velocity minus current velocity
- An animation name
- A path of waypoints
Why does seek alone orbit or overshoot its target?
- Because of floating-point errors
- Its desired speed is always MaxSpeed, so it never slows down before reaching the target
- Because MaxForce is too high
- Because the target moves
What does lowering MaxForce do to a vehicle's motion?
- It moves faster
- It turns in wider, smoother curves because its velocity can change less per second
- It stops steering entirely
- Nothing, only MaxSpeed matters