- Home /
Adding many BoxColliders
I'm trying to add about 30 box colliders at run-time.
Doing this with a for loop and gameObject.AddComponent() results in a noticeable lag-spike of 0.20 seconds.
Is there a way to group this to improve performance? I used to do this in my engine with BepuPhysics by creating the Compound in a separate thread, and dispatching the result back to the game thread to update the physics object. But since unity's API isn't thread-safe, I can't do this. But I also have no idea how to do it differently.
why not build a mesh at runtime of all the boxes you need and add it to a single mesh collider?
Shouldn't a mesh collider be slower than a box collider? And if I recall correctly: mesh-mesh collisions don't work in unity?
Answer by chillersanim · Jul 06, 2014 at 07:28 PM
Adding a component is ressource intensive.
That is because the component has to be registered in a global dictionary, and a collider has to be registered in the physics to.
I would recommend you not to use box colliders when you need so many, but instead create a mesh, that covers the used space and use a mesh collider.
You can either generate the mesh by code or make a mesh in your favourite 3D application.
Also try to replace many simillar components with one component, even if the replacement component is more complex.
In general, it costs more performance to iterate through all components than to calculate that one component.
Greetings
Chillersanim
That would be an option, but i'd have to split the mesh in convex parts, that would most likely end up in the same order of colliders. as the current box collider approach.