Search Unity

Performance: How expensive are Trigger Collisions? How many collision layers should be used?

Discussion in 'Physics' started by jschoener, Jun 6, 2020.

  1. jschoener

    jschoener

    Joined:
    Sep 20, 2019
    Posts:
    9
    Hi everyone,

    I'm currently working on an RTS Game. All movable objects have a rigidbody and several trigger colliders to detect other objects or for example check whether an object is in range or not. I use gameobjects with different layers for each type of object (units, animals, zombies, vehicles, static objects, etc.) and each type of trigger detection I need for that object type (touch trigger, near trigger, in sight trigger, etc.) so I can set the collision matrix to only detect collisions when needed and this way hopefully enhance performance. But since there are only 32 layers available and there are a lot of different objects that need different trigger colliders in my propject, I'll soon run out of layers, since I need them for other things as well. So my question is, how expensive is trigger collision? Does it make sense to avoid unnecessary collisions with different layers and the collision matrix in my case? Or should I let the trigger collisions be detected and analyze them with different tags or IDs to check what kind of trigger and what kind of parent object collided? And how well would this perform in comparison? Does it even make a difference?

    Thanks in advance :)
     
  2. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    989
    I've used triggers and colliders in many situation where hundreds of them were moving about all at the same time. Using layers will definitely help lower the cost of these interactions so I would suggest using them as much as possible. Obviously the layers are very limited and even worse, they function for visibility culling as well (something i think everyone can agree was a bad mistake that Unity made ages ago but likely won't be able to fix easily now) so you'll have to be sparing about how you use them.

    For my games I only have two possible 'teams', allies and baddies so it makes it manageable for me to use four layers: 1 for ally damage boxes, one for ally hit boxes, one for enemy damage boxes and one for enemy hit boxes. I also have a layer that can damage either group and a couple layers for things like walls that collide with anything, walls that only collide with living creatures (ally or enemy) and walls that only collide with projectiles. This can cut down on a lot of unneeded interactions and doesn't use up too many layers for me.

    If you end up having a lot of different teams then it will probably be better to simply have one layer for all hit boxes and one layer for all damage boxes: Then in your collision handler scripts you need some other means of identifying if you struck and enemy or ally. The upshot to this approach is that if you *wanted* friendly fire then this is how you'd do it anyway.