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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Attempt to reduce the amount of active Mesh Colliders

Discussion in 'Physics' started by PsychoDoc, Feb 3, 2016.

  1. PsychoDoc

    PsychoDoc

    Joined:
    Apr 28, 2015
    Posts:
    4
    Hello all!

    I have a brief question about Mesh Colliders. In my current project I wanted to reduce the amount of active Mesh Colliders at a time using the following “trick”:

    I have a lot of enterable buildings which all require Mesh Colliders. However, every building also has a Box Collider that is used as a trigger. When the scene is loaded, all Mesh Colliders are disabled and only enabled when the player triggers the Box Collider. Leaving the Box Collider would also disable the Mesh Collider, so effectively, there should be no more than one active Mesh Collider in the scene at a time.

    I was hoping to be less resource intensive this way. However, today I checked this assumption by activating all Mesh Colliders (which are quite a few) at once via script and found that there was no noticeable difference with respect to CPU time, or other performance indicators. So, are Mesh Colliders also taken in account when they are not enabled, or what could be wrong with my assumption? Does anyone have an explanation?

    Thank you in advance!

    PsychoDoc
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Mesh colliders like all other colliders are culled before checking collision. So if it's far away from objects being checked, it never checks actual collision anyway. The expensive parts are initial loading mesh collider(you do it anyway as they're just disabled - that means they're loaded in memory already) and checking against collision when other objects are close enough to mesh collider to not trigger culling.
    Usual checks for almost all physics engines:
    1. Is object that moved in same part of space as any other object it can collide with? (Spatial partitioning)
    2. Is object that moved did that through boundary box of object from previous step? (Boundary box)
    3. What is exact collision point between those objects? (Actual collision check - the slow part for mesh colliders)
     
    Last edited: Feb 7, 2016
  3. PsychoDoc

    PsychoDoc

    Joined:
    Apr 28, 2015
    Posts:
    4
    Thank you very much for your detailed reply – that really deepened my understanding of the underlying processes! So obviously, I kind of “reinvented” a mechanism that actually is already part of that process (boundary box)...