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

[Kinematics] Fix Tunneling and Collision Drops in Scripted Physics

Solution

mathematicscollisionphysicskinematicsdetection

Unity 2021.3.x - Unity 6.3.x

Published Thu, Apr 30

Issue

 When developers implement custom physics logic by directly modifying Transform positions, they bypass the internal sweep-testing provided by the Rigidbody system. Discrete collision checks, such as Physics.CheckBox or Physics.OverlapBox, only evaluate the start and end positions of a frame. At high velocities, the volume of a moving object may skip over a surface entirely in a single update, a phenomenon known as tunneling. Attempting to resolve this with Transform snapping or post-collision corrections often results in visual jitter and unstable physics interactions.

Explanation

To prevent tunneling in custom kinematic systems, the logic must shift from discrete overlap checks to swept volume detection.

  1. Calculate the intended movement vector for the frame based on current velocity and Time.deltaTime.
  2. Instead of moving the object immediately, execute a BoxCast using Physics.BoxCast along that movement vector.
  3. Use the RaycastHit.distance returned by the BoxCast to determine the maximum safe distance the object can travel before intersection.
  4. Update the Transform.position using this safe distance, ensuring the object stops exactly at the surface.
  5. If multiple collisions are expected within one frame, implement sub-stepping by dividing the movement into smaller BoxCast iterations.

Additional Tips

  • Always perform a BoxCast with a slightly smaller extents value than the actual collider to prevent ‘ghost’ collisions with adjacent parallel surfaces.
  • Combine the BoxCast with Physics.ComputePenetration to resolve any overlaps that occur when the system initializes inside another collider.
  • Use a layer mask within your BoxCast call to exclude the moving object itself and non-collidable triggers for better performance.

TL;DR

Scripted movement often causes collision tunneling due to discrete volume checking. This is reliably resolved by implementing swept volume detection via BoxCast and movement sub-stepping.


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.