How do i use Camera.CullingMask?

Ok I’d like to use Camera.CullingMask cause I know that’s one of the easy ways but I dont understand what unity means by << in Unity - Scripting API: Camera.cullingMask. Can someone help me?

It’s pretty rare to read or write the culling mask in a script (it can be modified from the editor)

<< is the C# “left shift” operator: Bitwise and shift operators - perform boolean (AND, NOT, OR, XOR) and shift operations on individual bits in integral types - C# reference | Microsoft Learn

It’s a bitwise operator that simply shifts the bits of a number to the left one time.

If your int called x (in binary) looks like this:
00000001

Then x << 1 will result in this:
00000010

However, in their example they are left shifting by 0, which doesn’t do anything. So the result of 1 << 0 is just 1. 1 is the bitfield representation of the first layer, because in binary it only has the last bit set (of 32 possible):
0000000000000000000000000000001

The second layer would be this (2 in binary)
0000000000000000000000000000010

The third layer would be this (which is 4 in binary)
0000000000000000000000000000100

A layer mask (which a culling mask is) is another bitfield that decides which layers are allowed to be seen by the camera. So if you have a culling mask like this:
1111111111111111111111111111111

Then every layer would be seen by the camera. But if you have a culling mask like this:
1111111111111111111111111111101

Then every layer except layer 2 would be visible by the camera.

If you’re unfamiliar with bitwise operators, I would just stick to using a public LayerMask field which lets you configure the layermask from the nice dropdown widget in the editor.

1 Like

Ok this helps alot. Thanks!