How to remove a single layer from cullingmask

Hi,

i have a culling mask with multiple Layers selected. for example: 0, 8, 9, 10

In game i create a second Camera that copies that culling mask, so i don´t / can´t predefine it in the inspector. The problem i can not solve is, how do i remove that single layer from my copied cullingmask. so the camera sees 0, 8, and 10.
I do not understand that bit shifting so i ask here, if someone can help me to remove that layer 9 from my copied culling mask.

Thanks in Advance.

1 Like

If you want to make layer 9 visible:

var newMask = oldMask | (1 << 9);

If you want to make layer 9 invisible:

var newMask = oldMask & ~(1 << 9);
31 Likes

Thank you, that did the job.

1 Like

This helped me too. Thanks!

1 Like

This worked for me too. Thanks makeshiftwings !

1 Like

Thanks :slight_smile: !

1 Like

Thanks
Wrote extension methods

    public static int AddLayerToLayerMask(this int layerMask, int layer)
    {
        return layerMask | 1 << layer;
    }
   
    public static int RemoveLayerFromLayerMask(this int layerMask, int layer)
    {
        return layerMask & ~(1 << layer);
    }

    public static void AddLayerToCullingMask(this Camera camera, int layer)
    {
        camera.cullingMask |= 1 << layer;
    }
   
    public static void RemoveLayerFromCullingMask(this Camera camera, int layer)
    {
        camera.cullingMask &= ~(1 << layer);
    }
3 Likes