- Home /
Partial meshcollider failure. Why does this happen?
I'm trying to generate terrain dynamically in my game. While doing this I encountered the 65000 vertex limit, so in order to work around this I have made a prefab and just instantiated the amount of meshes I need in order to cover my terrain.
the mesh algorithm looks like this:
visualMesh = new Mesh();
visualMesh.vertices = verts.ToArray();
visualMesh.uv = uvs.ToArray();
visualMesh.triangles = tris.ToArray();
visualMesh.RecalculateBounds();
visualMesh.RecalculateNormals();
meshFilter.mesh = visualMesh;
meshCollider.sharedMesh = visualMesh;
In 95% of the cases this works perfectly but sometimes when i change the size of a triangle or make a small change in the procedure to add triangles to this mesh, approximately half of the mesh looses its collision detection and players and other objects just fall through the terrain. The annoying part is that i don't know why. When i change the size of the triangles in one part of the mesh, the collision failure happens in an entirely different part of the mesh.
Answer by AlwaysSunny · Aug 31, 2014 at 01:00 AM
Other problems may exist that I can't see here, but I recall that colliders are finicky when you mess with them at runtime. IIRC, disabling / re-enabling them (even in the same frame) fixes certain issues like this.
Answer by Zart · Sep 01, 2014 at 10:56 AM
I tried fixing it by looping through the meshes and disabling/enabling them on every update but it didn't work.
the code looked like this:
void Update()
{
for(int i=0; i < meshes.Count; i++)
{
meshes[i].enabled = false;
meshes[i].enabled = true;
}
}
what other information do you need in order to figure out whats wrong with it?
Sorry for being unclear; I meant disable/re-enable the mesh collider component after having performed changes to the mesh which it uses. To my understanding, this "refreshes" the collider and updates it within the physics simulation.
Also it's possible the error could exist in the mesh generation code? Sometimes inconsistent collision on a mesh has something to do with improper normals, but generally such errors are visually apparent.
Is there a way of debugging for improper normals? $$anonymous$$y meshes are quite large and has some small triangles, so they may be hard to spot. If there is a triangle configuration that causes this I might be able to write my own debugging code.
$$anonymous$$y meshes are only created once right now but they will be dynamic later. I intend to create smaller meshes and re-rend the particular mesh that needs changing.
Your answer
![](https://koobas.hobune.stream/wayback/20220613161919im_/https://answers.unity.com/themes/thub/images/avi.jpg)