Using LayerMask in OnTriggerEnter?

Hello, is there any way to let’s say use layer or layermask as a trigger to OnTriggerEnter or Exit? I’m making a fps game and I currently have it set to the Player tag but I realized that if one guy goes into the trigger it will show up for everyone. Is there any way to prevent this? For example as mentioned earlier use maybe a layermask?

Happy to help

–

2 Answers

2

You can check against layers using other.gameobject.layer == LayerMask.NameToLayer (yourLayer).

Thanks alot :P

–

Working great! Thank You!

–

Using the bitwise operator you can able to find collisions with a layer mask.

         public LayerMask layerMask;
         private void OnTriggerEnter(Collider other) {
             if ((layerMask.value & (1 << other.transform.gameObject.layer)) > 0) {
                 Debug.Log("Hit with Layermask");
             }
             else {
                 Debug.Log("Not in Layermask");
             }
         }