Skip to content

Instantly share code, notes, and snippets.

@mstfmrt07
Created October 15, 2022 13:30
Show Gist options
  • Save mstfmrt07/edf1ca523c7fd5007a2661baa6d73ecc to your computer and use it in GitHub Desktop.
Save mstfmrt07/edf1ca523c7fd5007a2661baa6d73ecc to your computer and use it in GitHub Desktop.
A simple shortcut for checking if a layer is inside a LayerMask.
using UnityEngine;
public static class LayerExtensions
{
public static bool Contains(this LayerMask mask, int layer)
{
return (mask.value & 1 << layer) > 0;
}
}
//Example usage
public class PoorWarrior : MonoBehaviour
{
public LayerMask enemyLayer;
private int health = 100;
private void OnTriggerEnter(Collider other)
{
if (enemyLayer.Contains(other.gameObject.layer))
{
//Do something
GetDamage(10);
}
}
private void GetDamage(int damage)
{
if (health > 0)
{
health -= damage;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment