Search code examples
unity-game-engine

Unity 2020.3.48f mesh collider not working


i am making a game using unity engin and mesh renderer and mesh filter working but not mesh collider.mesh collideri have to use multiple box colliders instead mesh collider that is making the game lag and not optimized


Solution

  • Unity does not support non-convex MeshColliders on dynamic Rigidbodies. MeshColliders on moving rigidbodies have to be convex (which means no holes and no negative surface angle). When you tick the convex checkbox, Unity will calculate the convex hull of the original mesh.

    In the distant past (I think prior Unity 5) Unity did support non-convex MeshColliders on Rigidbodies, but they had limitations. Two non-convex colliders could never collide. At least one had to be convex. In the past it didn't matter which of the two colliders was convex. Since PhysX3 Nvidia completely dropped support for non-convex MeshColliders on dynamic rigidbodies.

    If you need concave colliders for rigidbodies, you have to use compound colliders. For compound colliders you can use primitive colliders (Box, Sphere, Capsule) or multiple convex MeshColliders.

    Since you mentioned nuts and bolts, those games usually use Unity's 2d physics system which is completely separate from the 3d one. The 3d physics system uses Nvidia's PhysX while the 2d physics system uses Box2D. Though Box2D also doesn't support non-convex polygon shapes as 2d colliders. The reasons are usually always the same as calculating collisions and contact points for arbitrary concave colliders is a really hard problem that doesn't have a simple solution and would require breaking up the shape into convex shapes. Such a process could be made automatic, but the results are not always optimal.

    There are third party assets on the asset store which can create a set of convex colliders for your mesh to resemble the original shape. Of course this comes with performance penalties as MeshColliders in general are expensive to compute. Primitive colliders are mathematically described shapes and always convex.

    I don't know how games like nuts and bolts exactly implement their mechanic, but I would assume they simply use convex colliders as well. The whole bolting mechanic may just be fix joints applied to the positions of the holes. When it comes to game programming it's all about how to get the desired result as simple as possible.