Search Unity

Enable component of other objects near player?

Discussion in 'Scripting' started by khos, Jul 20, 2017.

  1. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,487
    Hi,

    I would like to ask for your thoughts on how I can approach the enabling/disabling of components of other objects near player?
    E.g. if a cube is near a player to enable/add a collider to that cube, if the cube is too far then have the collider removed.. must the cube have a collider added so that the players knows about it, maybe set as trigger or the collider component disabled if out of range?

    I'm looking to do this for improving performance on my game that has 1000's of objects that need colliders..

    Thanks for any advice you can give me.
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    1000's of colliders will perform fine if they aren't dynamic rigidbodies. The colliders will already be doing what you're trying to do, have them be triggers or just leave them as colliders if you need them to be colliders... Having to turn them on and off through your own code constantly is going to be more of a performance incursion than simply having them sit there and exist.

    The only way I could see you really improving performance is if you simply sectioned groups of them off into "zones", so as your player approaches one zone, you would asynchronously start to set their collider components to enabled, and the ones in the zone you left to disabled when you leave it far enough. Adding/Removing components frequently is going to cause a lot of garbage and incur quite a performance hit.

    Colliders primarily become poor performance when:

    1. You have a very dense polycount collider that is being collided or raycasted against, since every vertex of that collider has to be processed. (But in this case that performance is only incurred during those actions, if nothing is interacting with it, it's primarily just data sitting in memory apart from its bounding-box values.)

    2. You have thousands of colliders with rigidbodies moving all around. (Once most stop moving, they go into sleep mode and performance goes back up somewhat)
     
    Last edited: Jul 21, 2017
  3. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,487
    Thanks Invertex for your info, much appreciated.