Created
August 8, 2018 22:08
-
-
Save beardordie/1d99b5c7cd287e0f77160c7d9e11c18e to your computer and use it in GitHub Desktop.
LayerMask check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LayerMasks are used often in Unity Technologies' 3D Game Kit to ensure a given interaction is only permitted/triggered by allowed objects. | |
Here are some code snippets they use often in the kit to achieve that check: | |
public LayerMask layers; | |
void OnTriggerEnter(Collider other) | |
{ | |
if (0 != (layers.value & 1 << other.gameObject.layer)) | |
{ | |
// layerMask check is met. Do stuff now. | |
} | |
} | |
And a class extension that can be used for LayerMasks is here: | |
public static class LayerMaskExtensions | |
{ | |
public static bool Contains(this LayerMask layers, GameObject gameObject) | |
{ | |
return 0 != (layers.value & 1 << gameObject.layer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment