So i get the concept of using layers. And I understand the idea of a layerMask, but I don't quite get it in practice. Specifically, the following syntax is not clear to me:

layerMask = ~(1 << 8);

What does the "~" and the "<<" do? Is the layermask a number? Anyone willing to take a minute or two to explain? Or to point out a good reference?

The layermask is a bit mask where every bit tells whether the corresponding layer is on or off. (Ie. bit 0 indicates whether layer 0 is on or off, bit 1 for layer 1, etc)

The <<, >> and ~ operators are bitwise operators. << and >> shift a bitfield or a number a certain amount of bits left or right and the ~ operators inverses it, changing all ones to zeroes and vice versa.

The expression ~(1 << 8) creates a bit mask with all bits set to one except bit number 8. We can break it down a bit more:

Let's start with the number 1. This is in itself a bitmask with only bit number 0 turned on. (The bits are numbered from 0 and up from the rightmost bit to the left.)

Then, we shift it to the left by 8 places using the << operator, which gives us a mask with all bits set to zero, except bit number 8.

And at last we apply the ~ to the result in order to invert the bits in the mask, turning it to a mask with all bits set to 1 except bit number 8.

That was very helpful! I understand the concept now.

To take it a step further, how do you generate a layermask (or cullingmask) number if you have multiple layers you want to exclude? I assume this involves some binary math and converting that to an integer.

For instance if I wanted layers 8 and 9 to be invisible to the camera, I know this number would be 64767 because I output the camera's cullingMask property while playing the game with layers 8 and 9 de-selected. I'm assuming this number is based on all 16 of Unity's available layers. But how would I calculate these numbers from scratch when excluding multiple layers?

I hope this is clear. Let me know if i'm not making any sense and i'll try to clarify.