[Physics2D] Prevent Collision Tunneling using Low-Level Physics 2D Thresholds
Solution
Unity 2023.1 - Unity 6.3.x
Published 26 days ago
High-velocity objects using the Low-Level Physics 2D system bypass collision detection, resulting in tunneling. This happens because the default configuration prevents Continuous Collision Detection (CCD) from engaging until a specific distance threshold is met, even if the fastCollisionsAllowed property is enabled on the PhysicsBody component.
Collision tunneling is mitigated by configuring the collisionThreshold property within the PhysicsBody or PhysicsBodyDefinition. This value determines the movement distance required before the simulation triggers Continuous Collision Detection.
By default, the collisionThreshold is set to 0.5. To fix tunneling for high-speed projectiles or fast-moving characters, follow these steps:
- Access the
PhysicsBodyinstance or thePhysicsBodyDefinitionused to create your bodies. - Set the
fastCollisionsAllowedboolean totrueto enable the CCD pipeline. - Reduce the collisionThreshold value. A value of 0.1 increases detection sensitivity, while 0 ensures that CCD is always active regardless of the travel distance.
- Ensure your shape extents are correctly defined, as the collisionThreshold is internally scaled by the size of the
PhysicsShape.
Additional Tips:
- Forcing collisionThreshold to 0 on every body can increase CPU overhead; only apply it to objects that realistically require high-fidelity collision.
- If your object still tunnels, verify that the
PhysicsWorldstep frequency is high enough to accommodate the velocity in your physics simulation. - Use
PhysicsBody.TransformWriteMode.Currentto ensure the UnityTransformstays synchronized with the low-level simulation results immediately.
TL;DR
Collision tunneling in Low-Level Physics 2D is resolved by reducing the collisionThreshold on the PhysicsBody or PhysicsBodyDefinition. Setting this value to 0 forces CCD to remain active for all movements.
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.