- Home /
How can I speed up assigning a mesh to a MeshCollider?
I am generating large meshes, getting close to Unity's ~65535 vertex limit. This doesn't take a long time - in fact, I can modify them in realtime.
However, when I call:
meshCollider.mesh = generatedMesh;
suddenly the whole program freezes for several seconds while this gets done.
Now, I'm assuming there's a lot of optimisation going on, like assigning vertices to an octree, and so on, but I'm wondering if there's any way to speed this up at all.
Alternatively, running the process in the background might work, since I don't need the collision geometry immediately after generating the mesh, but I'm not sure if this is possible in Unity.
Also, settings for the MeshCollider are:
Is Trigger: false
Material: none
Convex: false
Smooth Sphere Collisions: true
It can't be convex, because it's a hollow shape, and smooth sphere collisions are on because it needs to be as smooth as possible, since it's a driving surface.
Answer by Cherno · May 15, 2014 at 12:41 AM
When generating meshes at runtime, it's most often the part where the mesh gets assigned to the collider that is taking the longest time. There are no simple workarounds, it's just that way and not a bug. Consider smaller chunks and spreading their generation out over several frames to help speed thigns up.
Okay, thanks. Probably the best solution is to throw up a loading bar whenever they're needed. It'll happen when switching from an editor mode to playing, so it should at least make sense to the user.
Answer by zharik86 · May 14, 2014 at 07:08 AM
I faced such problem when worked over mazes. Walls was too much and they should be connected in one mesh. Then to use the MeshCollider component for this mesh. Time of generation of mesh isn't enough, but here creation of a collider took some seconds. The acceptable decision for abbreviation of time to find it didn't turn out. It was necessary to do a certain pause with a text about loading that not bad looked. However, in case of the test on the mobile device (Sony Experia Tablet Z) about 7 mesh with quantity of peaks ~ 65k loading about 10 seconds. Therefore if you have not any random generated mesh, it is best of all to use composite colliders (for example, some BoxCollider).
Thanks for the answer, although if you've run it through a translator I'm afraid it's made a mess of most of your sentences. I'm afraid composite colliders would destroy the smoothness of the mesh too much.
There are also nifty algorithms to create a seperate mesh for the meshcollider that has only as many vertices as needed by finding faces as large as possible. This would also speed things up since the resource-heavy part (assigning mesh collider) now deals with a mesh that has a smaller (possibly $$anonymous$$imum) number of vertices.
Check the "After playing $$anonymous$$inecraft..." thread on the forums.
@Cherno I'm afraid that's not really going to be possible, since the geometry of the mesh is designed around how it works as a collider. There aren't any flat surfaces to merge into a single face.
Your answer
Follow this Question
Related Questions
Accessing mesh vertices is extremely inefficient; any workarounds? 0 Answers
FBX mesh collider causing iOS to crash 2 Answers
Whether Collider need be combined? 0 Answers
Is it better to use Mesh Colliders on Mass Projects or Box Colliders? 2 Answers
Ray cast not hitting the backside of mesh collider attached to a spherical mesh object 0 Answers