Created
October 15, 2022 13:30
-
-
Save mstfmrt07/edf1ca523c7fd5007a2661baa6d73ecc to your computer and use it in GitHub Desktop.
A simple shortcut for checking if a layer is inside a LayerMask.
This file contains hidden or 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
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