Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Should I turn a collider off when it's not in use to save performance?

Discussion in 'Physics' started by Pontypants, Jul 27, 2023.

  1. Pontypants

    Pontypants

    Joined:
    Nov 18, 2017
    Posts:
    8
    I have a level with a lot of static 3D objects which all have mesh colliders on them. They are ONLY used for raycasting on, so no actual physics. I have set them to be triggers.

    Now, would it be good performance wise to disable the ones I don't currently care about? The only reason I have mesh colliders is so that I can calculate surface position of the mesh for the character IK. So any object that the character isn't close to could have its collider turned off.

    I'm just not sure if turning these objects on and off would actually save any resources. Will Unity constantly be checking for collisions on all these objects around the level or is it already optimized to not munch resources?

    Many thanks, if you have any suggestions for further reading I would happily accept links!

    Cheers!
    P
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    They need to have a collider to be a trigger or to be detected by raycasting. But if an object doesn't need a collider then turn the collider off so then it won't need to be considered by the physics engine.

    But repeatedly turning lots of colliders on and off can be very bad for performance because when enabling a collider it needs to be added to the physics engine's data structure which can be quite costly. So for example if you've tuned off 500 colliders to save performance then if you later enable them mid game you may notice the game freezes for a second. So in that situation it may be best to just leave them enabled all the time.

    I once made a game where I needed to spawn 1000 objects mid game and so my solution was to enable all the colliders gradually over a period of several seconds. It worked nicely!..
     
    Pontypants likes this.
  3. Pontypants

    Pontypants

    Joined:
    Nov 18, 2017
    Posts:
    8
    Thanks that's a great tip. I might need to look into something like that. I think if I turn them on as the player approaches there will never be an instance where I need to turn on 500 at once. But I will play around and check the profiler if there is even any sane reason to do this.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    To be clear from my pov, they are in themselves, "physics". Physics isn't limited to movement as your post is indicating. :)

    No physics system in the world would iterate all its objects per simulation step. Every such system places them in a spatial acceleration structure known as a Broadphase so they can be quickly located for things such as creating contacts or when devs perform queries. Indeed, some of the cost is to place them in this structure which drastically speeds up work later so adding/removing things is making your situation worse. Static colliders as you have here do nothing during the simulation step so leave them as they are.

    With all that said, rather than relying on what devs think might happen, it's super quick to add hundreds of Static colliders, spin up the profiler and see what the effect is per simulation step. :)
     
    Pontypants likes this.
  5. Pontypants

    Pontypants

    Joined:
    Nov 18, 2017
    Posts:
    8
    Thank you, excellent reply! So basically it's safe to just leave the colliders be, they won't cause any performance issues if they are left alone, even if they are active. This is what I was hoping for :) Thanks again!
     
    MelvMay likes this.