Deterministic Hierarchical State Machines in 2D Real-Time Combat Loops
Zero-allocation physics queries, input buffering, and multi-phase boss behaviors in Unity 6 and C#.
1. The Cost of GC Allocations at 60 FPS
In real-time game development, a single dropped frame ruins the tactile sensation of combat. At 60 FPS, the CPU has exactly 16.6 milliseconds to execute input reading, physics simulation, animation blending, and rendering. If the C# garbage collector triggers to clean up heap allocations, it blocks the main thread for 10-30 milliseconds, causing an unmistakable micro-stutter.
Standard Unity physics calls like `Physics2D.OverlapBoxAll` allocate a new array on the managed heap on every single frame. In Ashenfall, we banned allocating physics queries across the entire combat system, replacing them exclusively with pre-allocated buffer routines (`Physics2D.OverlapBoxNonAlloc`).
using UnityEngine;
public class AttackHitbox : MonoBehaviour
{
[SerializeField] private LayerMask enemyLayer;
[SerializeField] private Vector2 boxSize = new Vector2(1.5f, 1.0f);
// Pre-allocated static collider buffer to eliminate GC allocations
private static readonly Collider2D[] HitBuffer = new Collider2D[16];
public int ExecuteSlash(int damageAmount, Vector2 knockbackDirection)
{
Vector2 attackOrigin = transform.position;
int hitCount = Physics2D.OverlapBoxNonAlloc(
attackOrigin,
boxSize,
0f,
HitBuffer,
enemyLayer
);
for (int i = 0; i < hitCount; i++)
{
Collider2D target = HitBuffer[i];
if (target.TryGetComponent<IDamageable>(out var damageable))
{
damageable.TakeDamage(damageAmount, knockbackDirection);
}
// Clear reference to avoid holding memory references
HitBuffer[i] = null;
}
return hitCount;
}
}2. Input Buffering & Coyote Time
Player inputs rarely align perfectly with frame boundaries. If a player presses the 'Jump' or 'Attack' button 3 frames before landing on a platform, a naive controller ignores the input, making the game feel sluggish and unresponsive.
We engineered an input queue with a 0.12-second FIFO buffer window and implemented 'Coyote Time' (a 0.1-second grace window after walking off a ledge where jumping is still accepted). This guarantees that the game responds to player intent rather than mechanical frame precision.
3. Hierarchical Finite State Machines (HFSM)
Boss behaviors in Ashenfall are structured as Hierarchical Finite State Machines. Rather than giant switch statements with messy booleans, each boss state is an isolated class with explicit `Enter()`, `Update()`, `FixedUpdate()`, and `Exit()` lifecycle hooks.
The root state handles global parameters like phase transitions (e.g. transitioning to Enraged Phase when health drops below 50%), while sub-states manage micro-behaviors: Telegraphing, Dash Attack, Recovery Cooldown, and Area-of-Effect Slams.
4. Hit-Stop & Game-Feel Polish Systems
Combat feedback ('juice') transforms good mechanics into unforgettable gameplay. When an attack connects, our centralized `CombatFeedbackManager` coordinates three simultaneous actions:
1. Hit-Stop: A 0.06-second micro-pause freezing animator time scales, giving weight to the impact.
2. Cinemachine Impulse: A localized directional camera shake calibrated to the slash angle.
3. Particle Burst: Directional blood/spark emitters pooled in memory to avoid instantiation overhead.
Core Engineering Takeaways
- •Eliminate garbage collection micro-stutter by using pre-allocated Physics2D NonAlloc buffers.
- •Input buffering (0.12s) and Coyote Time (0.1s) make player controllers feel exceptionally responsive.
- •Hierarchical Finite State Machines (HFSM) cleanly decouple multi-phase boss behaviors from monolithic scripts.
- •Hit-stop micro-pauses and pooled particle feedback produce visceral combat weight.
View the complete architectural case study and source implementation.
Continue reading technical architecture insights.