Camera.cullingMask @ Runtime

Hi again. Alright heres the thing, i know that in order to assign cullingMask at runtime , you can use the bitshift operator as shown in the docs to assign any one single , or, all but one single … but what about multiple layers. Honestly im not to cool with concept / logic behind bits and bytes yet, but i know that this …

camera.cullingMask = ~(1 << 8);

can and does draw all layers except for 8. But what if i want to assign at runtime, layers 8 and 2 for eg : to the mask? Help would be grrreat! Thanks.

EDITED :

hmm on second thought , bah nevermind , prolly jsut easier to change the layer at runtime , and alter it between as needed.

camera.cullingMask = ~((1 << 8) + (1 << 2));

haha , ahh nice , that easy to add em together :stuck_out_tongue: Thaniks m8.

You shouldn’t be adding them together as that will most likely produce the wrong mask.
The bitwise OR operator is the one you need :smile:

camera.cullingMask = ~((1 << 2) | (1 << 8));

More info here: How do I use layermasks? - Questions & Answers - Unity Discussions

Ahh there we go , yea i though something was hokey about just adding them , cool now it makes much more sense. Thanks Aniani. BTW, opted to just change the layer the object was in, to a layer the camera was already currently ignoring. This solved everything just the same.

No problem, glad to hear it’s all sorted. For reference though, you could do the following to edit the mask at runtime:

var layerA : int = 1 << 2;
var layerB : int = 1 << 8;

// Add layerA to the mask
camera.cullingMask |= layerA;

// Remove layerB from the mask
camera.cullingMask = ~layerB;

Sweet , operator methods are not my strong point, those are great visual examples of how to use that. Nice , thanks for the updates :stuck_out_tongue: