- Home /
Large number static colliders performance impact in a scene?
Hi,
I have a question related to static colliders. I'm creating a big city in the scene where different building prefabs will be spawned. Now some buildings have a little complex geometry due to which I decided to use multiple box colliders as children of the building's gameobject. These colliders can go up to 20 to 25 static colliders per building.
My other option was to use mesh collider with convex checked but the convex colliders formed created some bad playing experience, for example, my character being blocked some distance away from the wall.
I also put these building colliders in a specific layer and removed their collision with each other in collision matrix as I need these colliders only to collide with player.
So imagine a city having roughly around 1000 static colliders in the scene. How badly will it affect the performance? By the way, target platform is windows and macOS standalone.
I apologise if this question has been asked before.
Answer by FlaSh-G · Sep 15, 2021 at 09:53 PM
First off: You don't need to disable collision between colliders that don't have a Rigidbody. Colliders are completely passive and don't care about collisions at all. It's Rigidbodies, CharacterControllers and Physics methods (like Raycasts) that look at colliders for collisions.
Next, physics engines have acceleration structures. They are data structures that create spatial partitions for your colliders. See, 2D and 3D space don't actually exist in your computer. There's just a pile of objects (like colliders) with numbers on them to define their position and shape in the virtual space. A Rigidbody that moves through the scene has to go through these colliders and check whether it intersects one of them with its own colliders. The acceleration structure allows the Rigidbody to only check the part of the scene it's actually in and ignore all colliders that are somewhere else. The Rigidbody will ask the physics engine for collision candidates and then checks the very small amount of colliders that are returned, one by one. This comes at an increased cost when moving colliders - when a collider's position changes, it might move into a different partition of the scene, so that needs to be checked. Since your colliders are marked as static, they never move and this cost is basically gone.
That being said, literally nothing comes at no cost at all. So while having quite a few colliders is not as bad an idea as one might think, the best thing you can do it try and do some profiling to see how well the machine can handle it.
I never got the opportunity to thank you for your response. Your explanation is really helpful. I setup static compound colliders on the buildings in the scene. So far I haven't faced any performance problem but as you said I will profile it much detail.
Your answer
Follow this Question
Related Questions
Hundreds of box colliders or 1 big mesh collider 1 Answer
Raycaste vs trigger Colliders & Rigidbody performance 1 Answer
Random Performance Spikes due to Physics.Processing in a static scene. 1 Answer
Optimizing Performance with tris 0 Answers
Performance hiccup on initial contact with rigid bodies 0 Answers