- Home /
Hundreds of box colliders or 1 big mesh collider
so I am making a maze game and performance is really important to me so I wanted to know if i should use a lot of box colliders or 1 big mesh collider for the walls and floors?
Answer by Chrissyeah_ · Sep 23, 2020 at 05:43 PM
Well, the most optimized solution I can think of is where it's all one big mesh and you've generated your own mesh. If I understand correctly, one mesh containing tons of merged objects is more performant than those individual meshes.
Another solution can be to try make bigger maze pieces so there are less colliders.
Also maybe if you have the colliders spawn dynamically as the player gets close.
*Edit, you could also look into combining mesh solutions.
Thanks for your answer. But how can I make the colliders spawn when the player gets close?
Hey man :D
So to add a box collider you can use
BoxCollider boxCollider = gameObject.AddComponent<BoxCollider>();
And I believe the box collider will attempt to scale up the box collider to your mesh. I think the success of this depends on how you set up your mesh.
If that's not working for you, you can manually set it with this code
$$anonymous$$eshRenderer renderer=gameObject.getcomponen<$$anonymous$$eshRenderer>();
boxCollider.center = renderer.bounds.center;
boxCollider.size = renderer.bounds.size;
You could put a big sphere collider on the player, Put a script on this object and make the collider a trigger. OnTriggerEnter you can get the object using tags. And add a collider to it. OnTriggerExit you can remove the collider.
Destroy (obj.GetComponent<BoxCollider>());
Im sure you can also use a mesh collider though I could not help you with the code. But if your mesh has a high poly count I would recommend not using a mesh collider.