Rigidbodies briefly fall through terrain then pop up. This is jarring and disrupts gameplay, especially for fast moving rigidbodies designed to slide or roll on the ground. I’ve reported this as a bug. Here’s the bug report title/number “IN-96728 - Terrain Collision missing frames” and here’s a link to an unlisted YouTube video I uploaded showing what happens https://youtu.be/KW7mrse41d8. Here’s another discussion I found talking about exactly my same issue from 5 years ago with no resolution Capsule collider occasionally partially sinks and bounces up.
I’m able to reliably reproduce this issue. I create a brand new project, create a terrain, set it’s width and length to 250 and its heightmap resolution to 513. Then sculpt the terrain so there’s some bumpiness. Ideally you sculpt a bumpy bowl in the ground which will act as a nice contained area for the capsule to slide around and show the bug. Then create a Capsule with a rigidbody. Constrain all the rotation axes on the Rigidbody so it remains upright. Create a new physics material with zero friction and “Min” for the friction combine. Then put the capsule on a slope in the terrain. Click play. As the capsule slides around on the terrain, I would expect it to smoothly slide on the surface of the terrain, but what actually happens is that the capsule will occasionally drop through the surface of the terrain up to .5 meters deep or so then pop back up and continue sliding. I’ve never observed the capsule completely falling through the terrain into the void. This seems to happen more often when the capsule is moving slowly. This seems to happen only when the terrain resolution is greater than the world units.
Does continuous collision detection change the behaviour? Also try playing around with Project Settings - Physics settings, particularly the number of iterations.
Just a blind guess, but have you tried disabling the Rigidbody constraints and using AddTorque for keeping the Rigidbody upright?
Since Unity 5 (IIRC) I’ve occasionally found that the Rigidbody constraints aren’t consistent or reliable. I wouldn’t be surprised if they’re messing up the collisions as well in the situations you describe.
Sorry for the delayed response here. I tried all the collision detection modes in the Rigidbody and increasing the iterations and other settings in the physics settings menu. None of that resolved the issue unfortunately.
Sorry for the late reply here. I disabled all the constraints and increased the angular drag in the rigidbody to 10 to at least make it stay on its “feet” a little longer. The bug still occurred. It did clip through terrain ground then pop up just as before and in the same spot as when I had the rotation constrains on.
Since Rigidbody movement on terrain colliders is quite important to my work, I’m going to try to write a script as a workaround. I’m thinking I’ll have a downward sphere cast with a radius just a bit smaller than the capsule’s radius. In my case, I keep the capsule’s rotation constrained always so I can be confident the spherecast will remain in the capsule. If it hits the ground and I determine the capsule bottom is farther away than the ground hit, then I know it’s clipping through the ground. Then I’ll use Rigidbody.MovePosition perhaps to move it up. I want to avoid assigning positions or velocities to my Rigidbody as I want to use believable physics forces as much as possible but in this case I might just need to do so. Hopefully this bug will get resolved though, or else I can learn if it’s something I’ve set up wrong. I’ll try to report back here if this workaround works for me.
I also just discovered that when the clipping occurs, the appropriate OnCollision methods get called. So when the capsule begins to clip, OnCollisionExit is called then there is no more OnCollision methods for some frames. Then when the capsule pops back up, OnCollisionEnter gets called and then OnCollisionStay while the capsule remains on the terrain surface.
Here’s a script that worked for me. I still notice the issue a bit here and there but it’s mostly resolved with this script. It assumes that the Rigidbody has rotation constraints on and the CapsuleCollider is upright. Hopefully Unity is able to fix this bug so workarounds like this aren’t necessary.
Edit: rather than using GetComponent now it just checks if hit.collider is TerrainCollider