EloiStree / HelloUnityKeywordForJunior

List of word nice to learn for Unity3D
0 stars 0 forks source link

Keyword: LayerMask #110

Open EloiStree opened 4 weeks ago

EloiStree commented 4 weeks ago

https://docs.unity3d.com/ScriptReference/LayerMask.html

EloiStree commented 4 weeks ago

🤖

using UnityEngine;

public class CheckLayerMask : MonoBehaviour
{
    // Assign your LayerMask in the inspector or programmatically
    public LayerMask layerMask;

    // Check if the GameObject is within the LayerMask
    public bool IsInLayerMask(GameObject obj, LayerMask mask)
    {
        // Shift the layer of the GameObject and check if it's in the LayerMask
        return (mask.value & (1 << obj.layer)) != 0;
    }

    // Example usage
    void Start()
    {
        GameObject obj = gameObject; // The GameObject you want to check

        if (IsInLayerMask(obj, layerMask))
        {
            Debug.Log($"{obj.name} is in the LayerMask.");
        }
        else
        {
            Debug.Log($"{obj.name} is NOT in the LayerMask.");
        }
    }
}