skip to content

A boss NPC's state machine flips between Approach and Attack every few frames at its attack range's edge; why, and how do you stop it?

level: middleimportance: must knowfreq 66%

answer

  1. one threshold, two directions
  2. noise crosses the line
  3. separate enter and leave values
  4. commit for a minimum time

basics

~20 s

Both transitions share one distance threshold, so small jitter near it crosses the line each tick. Add hysteresis, entering Attack below one distance and leaving only above a larger one, plus a minimum dwell time so a state commits.

solid answer

~40 s

The flip is **oscillation**: `Approach` goes to `Attack` when `distance < 3 m` and `Attack` goes back when `distance >= 3 m`, so any noise near 3 m (the player strafing, the boss's own lunge, measuring to a moving bone) crosses the threshold in both directions. The fix is **hysteresis**: enter `Attack` below 3 m but leave only above, say, 4 m, so the band between them absorbs jitter. The band must be wider than the per-tick noise and narrower than the attack's reach, or the boss swings at players it cannot hit. Add a **minimum dwell time** or let `Attack` leave only at interruptible points, such as between swings, and confirm the cause by logging each transition with the guard inputs that fired it.

go deeper

for a junior

Recognise oscillation as two transitions sharing one threshold and know the word hysteresis: one value to enter, a different value to leave.

for a middle

Explain how to size the band against measured noise and the attack's reach, and when to add a dwell time or interruptible points instead.

for a senior

Diagnose from a transition log rather than guessing thresholds, spot cycles between two different guards, and keep overriding transitions such as death outside any commitment rule.

for a principal

At lead level this becomes a shared convention: thresholds with hysteresis and dwell times as standard parameters on transitions, with transition logging built into every NPC machine.

## What the symptom is **Oscillation** (also called flicker or dithering) is a state machine switching between two states on alternate ticks or every few ticks. For a boss with `Approach` and `Attack`, the visible result is a monster that starts a swing, cancels it, steps forward, starts again, and never lands a hit. Animation blending makes it look worse: each switch restarts a transition between poses. ## Why it happens The usual cause is that the two transitions are mirror images around **one threshold**: - `Approach` leaves for `Attack` when `distance < 3 m`. - `Attack` leaves for `Approach` when `distance >= 3 m`. Any input that wobbles around 3 m now drives a switch per wobble. Typical sources of wobble: - The player strafing or dodging right at the edge. - The boss's own attack moving it: a lunge, a recoil, a pushback on hit. - Measuring distance to a point that moves with animation, such as a hand bone, rather than a stable root position. - Inputs sampled at different times, so two guards see slightly different worlds. Oscillation can also come from two guards that are each other's trigger, such as "leave `Attack` when the target is behind me" while the turning done in `Approach` puts the target behind again. ## Fix 1: hysteresis **Hysteresis** uses two thresholds, one to enter and a larger one to leave: | Transition | Single threshold | With hysteresis | |---|---|---| | `Approach` to `Attack` | distance below 3 m | distance below 3 m | | `Attack` to `Approach` | distance at or above 3 m | distance above 4 m | | Band where nothing changes | none | 3 m to 4 m | Inside the band, whichever state is current stays current. The design work is choosing the band: 1. It must be **wider than the per-tick noise**, including the boss's own movement during an attack, or the noise still crosses both lines. 2. It must be **narrower than what the state can actually do**. If the swing reaches 3.2 m and the leave threshold is 4 m, the boss stands in `Attack` and swings at a player 3.5 m away, missing every time. 3. It is a per-transition parameter, set for this NPC and this attack. There is no universal width. ## Fix 2: commitment and dwell time Hysteresis handles noisy inputs; **commitment** handles states that should not be abandoned halfway: - A **minimum dwell time**: once entered, a state ignores its ordinary exits for a set time, such as half a second in this scenario. Death and stun still override it. - **Interruptible points**: `Attack` checks its exit only between swings, so a swing in progress always completes. - A **re-entry cooldown** on the transition back, so the boss cannot re-enter `Attack` within a short time of leaving it. These delay reactions, so they trade responsiveness for stability. Keep overriding transitions such as death outside them. ## Fix 3: steadier inputs Measure from stable positions (the root, not a bone), sample the inputs once per tick and share the values across all guards, and smooth an input over a few ticks when it is inherently noisy. Smoothing adds lag, so prefer hysteresis when the noise is small. ## Confirming the diagnosis Before changing thresholds, confirm the cause: - Log every transition with the tick number, the source and target, and the guard's inputs (here, the distance) at that moment. - Plot the distance over the flicker window. Values straddling one number confirm a shared threshold; a clean distance with flicker anyway points to a different guard pair. - Check whether the switches line up with the boss's own animation events, which would show that the attack itself is moving the input. ## Pitfalls - Widening the band until flicker stops without checking the attack's reach, which trades flicker for whiffs. - Adding a dwell time to every transition, including death, so the boss survives a killing blow for half a second. - Fixing the symptom in animation blending while the machine still switches state on every wobble.

  • How do you choose the width of the hysteresis band?
    Measure the noise: log the distance input while the boss attacks a strafing player and find how far it moves per tick, including the boss's own lunge. The band must exceed that. Then check it against the attack's reach: the leave threshold should not exceed the distance at which the swing can still land, or widen the reach instead.
  • Why must death and stun bypass a minimum dwell time?
    Dwell time exists to stop low-stakes flicker between ordinary states. A killing blow or a stun must take effect immediately, or the boss keeps swinging after it should be dead or dazed, which reads as a bug. Apply dwell time to the ordinary exits of a state only, and keep overriding transitions outside it.
  • Can oscillation happen even with hysteresis on every threshold?
    Yes. Two guards on different inputs can drive each other: leaving `Attack` when the target is behind, while `Approach` turns the boss so the target ends up behind again. Hysteresis on one number cannot fix a cycle between two conditions; log the transitions to find the pair, then break the cycle by commitment or by changing what one state does.

A home thermostat that heats below 20 degrees and stops at 21: with one setting for both, the boiler would click on and off all day as the room drifts by a fraction of a degree.

  • Flicker is an animation bug, fixed by longer blend times.
  • Hysteresis means using the same threshold but checking it less often.
  • A wider hysteresis band is always safer.
  • A minimum dwell time should apply to death and stun too.
  • Oscillation only happens when guards are tested in the wrong order.