Course contentsShow
← Unity Game Development
Lesson 186 of 8283. Physics and InteractionFree lesson

Mitigating Tunneling With Continuous Collision Detection

Students will configure collision detection modes to prevent fast-moving characters from passing through thin walls and colliders.

In this lesson, you will learn how to prevent fast-moving objects from "tunneling" through thin walls by configuring the Collision Detection mode on your Rigidbody components.

The Tunneling Problem

In Unity’s physics engine, Discrete collision detection—the default setting—checks for collisions only at specific intervals when a frame updates. If an object is moving fast enough, its position in one frame might be on one side of a wall, and its position in the next frame might be completely on the other side. Because the object never technically "overlays" with the wall during a frame calculation, the physics engine never registers a collision. This phenomenon is known as tunneling.

Continuous Collision Detection (CCD)

To fix this, we use Continuous Collision Detection. Instead of just checking if an object is overlapping an object at the end of a frame, CCD calculates the "time of impact" by sweeping the object’s shape along its trajectory. If the path between the previous position and the new position intersects with a collider, Unity will detect it, even if the object is moving at extreme speeds.

How to Configure CCD

To enable this on your fast-moving objects:

  1. Select your object in the Hierarchy.
  2. Locate the Rigidbody component.
  3. Look for the Collision Detection dropdown menu.
  4. Change it from Discrete to either Continuous or Continuous Dynamic.
  • Continuous: Use this for fast-moving objects (like a projectile) that need to collide with Static (non-moving) colliders.
  • Continuous Dynamic: Use this if your fast-moving object also needs to collide with other moving objects that have CCD enabled.

Note that CCD comes with a performance cost, as the engine performs more complex math to track these trajectories. Only enable it on objects where tunneling is actually occurring, such as bullets, high-speed vehicles, or teleporting characters.

// Example: Setting collision detection via script
void Start() {
    Rigidbody rb = GetComponent<Rigidbody>();
    // Set to Continuous to ensure the object doesn't pass through walls
    rb.collisionDetectionMode = CollisionDetectionMode.Continuous;
}

Key Takeaway: Use the Continuous collision detection mode on high-speed Rigidbody objects to force the physics engine to calculate potential impacts along an object's path, effectively preventing them from skipping over thin colliders.