Physics & Hit Detection (Performance Architecture)

The "No Collider" Philosophy

One of the most common questions about Enemy Masses is: "Where are the Colliders?"

The answer is: There aren't any.

The Problem with Unity Physics

Unity's built-in physics engine (PhysX) is incredible, but it has limits. Every Rigidbody and Collider you add to a scene requires CPU time for:

  • Collision detection (Broadphase & Narrowphase).

  • Trigger checks.

  • Physics solving (resolving overlaps).

  • Transform synchronization.

When you have 50 enemies, this is negligible. When you have 5,000 enemies, the physics engine alone can bring high-end PCs to a crawl (single-digit FPS).

The Enemy Masses Solution

To achieve massive scale (thousands of units), Enemy Masses completely bypasses the standard physics engine for crowd agents.

  1. No Rigidbodies: Agents do not have Rigidbody components.

  2. No Colliders: Agents do not have Capsule or Box Colliders.

  3. Pure Math: All interactions are calculated using lightweight mathematical checks.


Hit Detection: Distance vs. Colliders

When dealing with thousands of units, the method you choose for hit detection determines your game's performance ceiling. Enemy Masses supports two methods, but strongly favors one.

This is the default and primary method used by Enemy Masses. Instead of relying on Unity's physics engine (PhysX), we use pure mathematics to determine if an attack hits an enemy.

How it Works (Virtual Hit Boxes)

Every enemy agent is defined by a Virtual Hit Box, configured in the EnemyCrowd asset:

  • Radius: The width of the agent (e.g., 0.5m).

  • Height: The height of the agent (e.g., 2.0m).

When you fire a projectile or swing a sword:

  1. The system queries the Spatial Hash Grid (a lightweight bucket system) to efficiently find agents near the attack point.

  2. It performs a mathematical check: Distance(Attack, Agent) < (AttackRadius + AgentRadius).

  3. If true, a hit is registered.

Why use this?

  • Extreme Performance: Checking Vector3.DistanceSquared is thousands of times faster than Physics.OverlapSphere or Raycast against thousands of colliders.

  • No Overhead: No Rigidbodies or Colliders are required on agents, saving massive amounts of memory and CPU time.

  • Deterministic: The math is consistent and doesn't rely on the physics engine's update rate or jitter.

Setup in EnemyCrowd

One Click Solution: Click on Initialize Default Humanoid Zones to quickly setup virtual Hitboxes for Torso, Head, and Legs.

2. Collider-Based Detection (Legacy / Deprecated)

It is technically possible to add a CapsuleCollider and Rigidbody (set to Kinematic) to every agent prefab. This allows standard Unity triggers (like OnTriggerEnter) to detect them.

Why avoid this?

  • Performance Cost: Updating 5,000 Colliders every frame requires significant CPU time for the PhysX Broadphase update, even if they don't collide with each other.

  • Memory: Physics structures consume extra RAM per agent.

  • Instability: Physics engines can struggle with high-density crowds, leading to jitter or performance spikes.

Warning: Collider-based hit detection is considered Legacy and will likely be Deprecated in future versions of Enemy Masses. We strongly recommend designing your combat system around the Distance-Based approach (using the provided PlayerSkillManager or CrowdHitReceiver systems).

Summary Comparison

Feature
Distance-Based (Virtual)
Collider-Based (Physics)

Performance

Extremely High (5k+ units)

Low (500-1000 units)

CPU Cost

Negligible

High (PhysX Overhead)

Setup

Automatic (Radius/Height settings)

Manual (Add Components)

Accuracy

High (Mathematical)

High (Physics Engine)

Future Support in Enemy Masses

Core Feature

Likely Deprecated


"Fake Physics" & Impulses

Since agents don't have Rigidbodies, we cannot use AddForce() or AddExplosionForce(). Instead, we use a custom Fake Physics system to simulate these effects visually while keeping the agent grounded on the NavMesh.

How Knockback Works

When an explosion hits a crowd:

  1. We calculate a Vector from the explosion center to the agent.

  2. We apply a NavMesh Velocity override to the agent.

  3. The agent slides across the ground to the new position.

How "Knock-up" (Lift) Works

If you want to launch enemies into the air:

  1. The Agent's Logical Position stays on the ground (NavMesh).

  2. The Agent's Visual Mesh is offset vertically using a parabolic curve.

  3. To the player, it looks like the enemy flew through the air and landed, but logically they never left the NavMesh.

Why?

  • Performance: No physics solving required.

  • Stability: Agents never get stuck inside walls or fall through the floor.

  • Pathfinding: The NavMeshAgent never loses its state.

Ragdoll Integration

A common concern is that "Fake Physics" means unsatisfying deaths. This is not the case.

Enemy Masses bridges the gap between simulation and physics when an agent dies:

  1. While Alive, the agent accumulates "Fake Physics" velocity (Knockback + Lift) for movement.

  2. The moment the agent Dies, if Ragdolls are enabled, the system spawns a real Ragdoll prefab (which has Rigidbodies).

  3. We take the accumulated Fake Velocity and convert it into a real Physics Force (using Rigidbody.AddForce or AddExplosionForce).

Result: If you hit an enemy with a massive explosion, they will fly backward using fake physics while alive. If that explosion kills them, their ragdoll will inherit that momentum and continue flying through the air using real physics.

This gives you the best of both worlds:

  • Alive: High-performance, stable simulation for thousands of units.

  • Dead: Satisfying, physics-based ragdoll impacts for the units that are dying.


Trade-offs

This architecture is what allows Enemy Masses to render and simulate 10,000+ units where standard approaches fail at 500, but it comes with trade-offs:

  • No Physics Stacking: Enemies will not pile up physically like ragdolls (until they die and spawn a real ragdoll).

  • No Environment Interaction: Enemies won't push physics crates or trigger standard Unity Triggers/Colliders.

  • Simple Collision: Agents use simple radius checks (cylinders), not complex hitboxes.

Last updated