Hysteresis is a concept many devs implement intuitively through buffer zones or different thresholds for entering and exiting states. For example, using a wider detection radius to lose aggro than to gain it prevents enemies from rapidly switching states when the player is near the edge of detection.
Hysteresis means a state doesn’t change the instant conditions flip. Instead, it waits until the change is clear. For example:
- Enemy AI state transitions (patrol ↔ chase ↔ search): require clearer criteria to switch back so enemies don’t rapidly oscillate when player skirts edge of detection.
- Joystick deadzone: use different enter/exit thresholds so slight stick drift doesn’t repeatedly flip input state.
Aggro Example
public class Enemy
{
public bool hasAggro = false;
public void Update()
{
float distance = Vector3.Distance(transform.position, player.position);
// Lost aggro - player escaped
if (hasAggro && distance > 10f)
{
hasAggro = false;
return;
}
// Gained aggro - player too close
if (!hasAggro && distance < 6f)
{
hasAggro = true;
return;
}
// Distance between 6-10: keep current state
}
}The magic: Enemy gains aggro at 6 units but only loses it at 10+ units. That 4-unit buffer zone eliminates jittery state switching.