Errors about mesh collider when CONVEX is set to true

I use the following code to create a mesh for convex mesh collider, but it causes some errors.

        GameObject go = new GameObject();
        MeshCollider collider = go.AddComponent<MeshCollider>();

        Mesh newMesh = new Mesh();
        newMesh.name = "test";

        Vector3[] vertices = new Vector3[] {
            new Vector3(0.0f, 0.0f, 0.0001f),
            new Vector3(0.1f, 0.0f, 0.0f),
            new Vector3(0.09f, 0.05f, 0.0000f),
            new Vector3(0.05f, 0.07f, 0.0000f),
            new Vector3(0.02f, 0.09f, 0.0f),
            new Vector3(0.0f, 0.1f, 0.0f),
        };

        int[] tris = new int[] {
            0, 1, 2,
            0, 2, 3,
            0, 3, 4,
            0, 4, 5
        };

        //int[] tris = new int[] {
        //    1, 2, 3
        //};

        newMesh.vertices = vertices;
        newMesh.triangles = tris;
        newMesh.RecalculateBounds();

        collider.sharedMesh = newMesh;
        collider.convex = true;

Errors:

  • ConvexHullBuilder::CreateTrianglesFromPolygons: convex hull has a polygon with less than 3 vertices!
  • Gu::ConvexMesh::loadConvexHull: convex hull init failed! Try to use the PxConvexFlag::eINFLATE_CONVEX flag. (see PxToolkit::createConvexMeshSafe)
  • Failed to create Convex Mesh from source mesh “test”. Source mesh is likely have too many smooth surface regions. Please reduce the surface smoothness of the source mesh. Alternatively turn on Inflate Mesh and increase the Skin Width sufficiently for this mesh.

I think the errors are due to all vertices of mesh are nearly on the same plane . But, there are still some strange behaviors:

  • If I set the first vertex to (0.0f, 0.0f, 0.0f), there is no error.

  • If I use commented “tris” variable to create the mesh, so the mesh contains only one triangle, but there are same errors.

Can somebody explain these two strange behaviors for me? Thank you.

That’s not a convex shape and can’t be used as a convex collider. Also the scale is very small which physics doesn’t really like.

–Eric

The mesh of convex mesh collider dosen’t need to be convex, physics engine should create it automatically, if I understand correctly.

It needs a 3D shape to work from, though, not a 2D shape. Or rather, some form of enclosed volume. If you create a similar shape in a 3D app and import it, and try to make a convex mesh collider from it, you’ll see that you don’t get anything usable. Out of curiosity I did some experimenting with that just now, and the results ranged from nothing at all, to the same error that you got, to (best case) a generic box that wasn’t even close to the actual size of the mesh. So yeah, you’re going to have to extrude the mesh yourself if that’s what you were aiming for with the convex collider.

–Eric

But, why it’s correct when I set the first vertex to (0.0f, 0.0f, 0.0f)? It’s also a 2d shape.

Just lucky I guess? The boxes I got where nowhere close to the size of the mesh. I think it’s just a bad idea to attempt to make a convex mesh from a 2D shape since the results aren’t at all consistent, and are presumably undefined.

–Eric

OK, it seems that I should not use 2d shape to create convex mesh collider like you said. Thank you.