Why do bullets pass through walls?

Physics never saw them inside the wall.

Why do bullets pass through walls?

The problem

A fast body, checked at stops.

void Fire(){    Rigidbody b = Instantiate( bullet,        muzzle.position, muzzle.rotation );     b.AddForce( muzzle.forward * 400f, ForceMode.Impulse );}

Physics checks the bullet 50 times a second. At 400 m/s it moves 8 m between checks.

What it costs

How far it jumps per physics step.

Distance moved between two checks, at the default 50 a second:

Walking, 5 m/s
0.1 m
Car, 50 m/s
1 m
Bullet, 400 m/s
8 m

Anything thinner than the jump can be skipped entirely.

The fix

Check the path, not just the stops.

Continuous collision sweeps the body along its path between steps, so a thin wall is still found.

Discrete→checks the stops
Continuous→checks the path

The fix in code

Only on the fast ones.

void Awake(){    var rb = GetComponent<Rigidbody>();    rb.collisionDetectionMode =        CollisionDetectionMode.Continuous;}

It costs more, so set it on bullets, not on every crate.

Watch out

Hitting things that move?

Continuous is for fast bodies against still walls. Moving targets need Continuous too.

Fast vs still walls
Continuous
Fast vs moving
ContinuousDynamic
Instant hits
a raycast

The rule

Small and fast? Make it continuous.

Bullets, fast balls
Continuous
Everything else
Discrete
Instant hits
Raycast