Unity : is it possible to configure the scene for trigger colliders to only work with one layer?

Viewed 1221

Unity already manages the collisions and triggers well but I would like to know if I can optimise triggers to only react to a layer ?

Currently if I have many different objects with colliders in a scene I need to do :

void OnTriggerEnter (Collider other) {
    if (other.gameObject.layer == myLayer) {
        // do stuff here
    }
}

But this means that the collisions where calculated with ALL of the colliders witch does not seem efficient.

If I want a trigger to react to the player and to the player only, is there a way to tell it to ignore all of the other layers to avoid unnecessary computing ?

2 Answers

Yes, using the Collision Matrix as described in the manual here.

  • Add a new layer mask (or just use the one you already have)
  • Uncheck all of the layers it can collide with except the "Player" layer
  • Change the game object's layer to the new modified layer
  • Now that object can only collide with the player, and only get triggered by the player (provided one of them has a rigid body component)
Related