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.
