UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

physics

[Physics2D] Prevent Collision Tunneling using Low-Level Physics 2D Thresholds

Solution

physicscollisioncicdoptimization

Unity 2023.1 - Unity 6.3.x

Published 26 days ago

Issue

 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.

Explanation

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:

  1. Access the PhysicsBody instance or the PhysicsBodyDefinition used to create your bodies.
  2. Set the fastCollisionsAllowed boolean to true to enable the CCD pipeline.
  3. 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.
  4. 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 PhysicsWorld step frequency is high enough to accommodate the velocity in your physics simulation.
  • Use PhysicsBody.TransformWriteMode.Current to ensure the Unity Transform stays 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.