I’m developing a rigid body character controller and it is nearly finished, with head bobbing, speed transition between running, walking, crouching, etc. But It is now time to address a problem I’ve been having since the day I started developing it.
I’ve created a simple Terrain using Unity’s built-in terrain tools to test the character controller and all works well. Unfortunately, when moving, sometimes my controller briefly sinks about 5% to 30% through the ground before bouncing up, this happens in a fraction of a second in particular places of the terrain map, in very small areas. I’ve reconstructed the terrain but the problem persists. It really doesn’t matter if the terrain is flat or irregular, it will happen in specific places of the terrain and I can trigger the problem whenever I move across said places.
It feels like those areas, with the radius smaller than the capsule itself, have no collisions. It’s not like the capsule teleports bellow ground level, the capsule simply falls into those “holes” before bouncing up.
Is the capsule collider passing significantly through the terrain collider? Or does the terrain dip, in which case it makes sense for the capsule collider to fall into the hole a bit?
It’s completely normal, in Unity’s physics, for two colliders to overlap temporarily as a result of a collision. Unity won’t prevent the overlap. Instead, it will spent time in the subsequent FixedUpdates pushing the objects apart so they stop overlapping. So in that sense, if you really did fall into a hole, it would be normal for the capsule collider to go through the terrain collider partially, before being pushed up out of it by the physics system. So the question is, just how far into the terrain is the capsule going? Maybe you can show some screenshots.
When the capsule goes through, does it instantly just go through? Or is it the result of falling some distance? This doesn’t occur on flat ground? Have you altered any of the Physics settings on this project, like the fixed timestep, or the gravity? Does your controller move via AddForce, or are you using Rigidbody.Move? Are you doing something different on slopes that’s perhaps moving the character more vertically than you were expecting?
I’ll make a GIF so it’s more clear what is happening, as soon I get the chance. However, I can say that the only function I have that changes physics, is the jump function, that adds a vertical force when a key is pressed. As I described in the opening post, the issue occurs whether if the surface is flat or irregular, it really doesn’t mater. It happens simply by moving around and passing on specific places in the terrain, without jumping or anything. I’m using MovePosition(). All will be clear once I upload an example.
Here is a quick video showing what happens:
smoggyreadyfulmar
I’m thinking on removing the rigid body from the PlayerGame object and move it to another folder so that it doesn’t affect the camera. The collision problem will remain but at least the camera won’t be affected by it
That’s pretty weird. And you say that part is basically flat, as far as the terrain goes? No dips in that one problem spot? The fact that it repeatedly happens at one spot, but not all spots, makes me think it’s not the movement controller, and that it’s probably related to the terrain, but I don’t have a lot of familiarity with Unity terrain.
As far as your rigidbody goes, just be careful about moving the rigidbody to another component. If you put the rigidbody on a child of the collider, you may find that it moves without the collider, or you’ll see weird results.
At this point I don’t have any idea what’s causing this, but maybe you can be extra sure about the movement by manually placing the character in various places, or dragging the transform around, to see if it ever does this when you’re not moving via your movement script. For now, I’d just try to tease apart all the different things that could be happening, so you can narrow it down and hopefully reach an insight into the root cause.
Yes, that specific terrain area is nearly flat and I can’t see any dips on it.
Yes, I’m aware of that. Thank you for noticing it though.
I think I will just move the rigidbody one level down along with its collider and move on. Perhaps the issue will even go away this way. If it doesn’t at least the problem won’t be visible by the player.
I think if you do that, the collider will move around without moving the rest of your game object. So, for example, if the renderer is on the parent object, and the collider and rigidbody are on the child, the collider will move around, but the renderer won’t. Maybe you’ll have better luck, but I’ve never had a good experience placing the rigidbody on a child object, except for joint situations.
Yes, exactly. Always put the rigidbody on the root of the moving object. An object with a rigidbody should never have any other rigidbody in it’s children.
The box collier was triggering some side effects with the camera, due to the nature of its shape colliding to the ground. So, I just replaced it with another tiny capsule. I really would like to know what is causing this weird issue, but…