Skip to content

Instantly share code, notes, and snippets.

@beardordie
Created August 8, 2018 22:08
Show Gist options
  • Save beardordie/1d99b5c7cd287e0f77160c7d9e11c18e to your computer and use it in GitHub Desktop.
Save beardordie/1d99b5c7cd287e0f77160c7d9e11c18e to your computer and use it in GitHub Desktop.
LayerMask check
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