Character glitches through BoxCollider when intersecting with MeshCollider?

Hi,
I’m working on an top down fixed camera 3D game. I just tried to wall off the area with empty GameObjects with BoxColliders. In some areas, these now intersect with MeshColliders of larger objects like rocks.
While these BoxColliders work fine on their own, my character sometimes glitches through them when standing on another MeshCollider and then moving at a certain angle.
I could nicely reproduce this by setting up a BoxCollider in the middle of the scene and placing a large rock with a MeshCollider halfway into it. I can’t walk straight through the wall, but after climbing the rock from a certain direction I can then reliably pass through the BoxCollider.

My charecter uses a capsule collider and a Rigidbody and is moved via Rigidbody.MovePosition
The ground uses as MeshCollider, too.

I read several times that MeshColliders can be “problematic”, but could not find anything that really fits my issue.

It seams like the rigidbody tried to collide with both and, as you said, since mesh colliders are problematic he partiallu went through the mesh collider but still colliding with the part outside and the box collider.
Am I right?

If that is the case put continous collision detection on your rigidbody to prevent it.

Please uplaod some image of the error so I can help you better.

foolishtestyhedgehog

I hope that illustrates the problem. The character is walking on a mesh collider, the rock also has a mesh collider. When the character gets on the rock, it just glitches through the box collider.

Edit: Continuous collision detection does not fix it, unfortunately.

In 3D MovePosition is meant for Kinematic bodies, not Dynamic ones. AFAIK in 3D, for a Dynamic body it just instantly moves it to that position (no actually movement, continuous or not) which might mean it’s overlapped. The solver will then desperately try to fix that overlap but you’re probably doing MovePosition again etc.

What MelvMay said.


MovePosition (3D) translates the body.

A quick fix for this: set the velocity directly based on the target position and the current one:

rigidbody.velocity = (targetPosition - rigidbody.position) / Time.fixedDeltaTime;

Or maybe add an extension method (this will affect all Rigidbodies):

public static class CustomPhysics
    {
        public static void MovePosition(this Rigidbody rigidbody, Vector3 position, bool detectCollisions)
        {
            if(detectCollisions)
                rigidbody.velocity = (position - rigidbody.position) / Time.fixedDeltaTime;
            else
                rigidbody.MovePosition(position);
        }

    }
}

Now you can use it anywhere:

rigidbody.MovePosition(targetPosition, true);
1 Like