[Kinematics] Fix Tunneling and Collision Drops in Scripted Physics
Solution
Unity 2021.3.x - Unity 6.3.x
Published Thu, Apr 30
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.
To prevent tunneling in custom kinematic systems, the logic must shift from discrete overlap checks to swept volume detection.
- Calculate the intended movement vector for the frame based on current velocity and
Time.deltaTime. - Instead of moving the object immediately, execute a BoxCast using
Physics.BoxCastalong that movement vector. - Use the
RaycastHit.distancereturned by the BoxCast to determine the maximum safe distance the object can travel before intersection. - Update the
Transform.positionusing this safe distance, ensuring the object stops exactly at the surface. - 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.ComputePenetrationto 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.