The layer is a number from 0 to 31. It corresponds to each of those named layers.
A layer MASK is a 32-bit bitfield that says which of these you want to ignore. This enables you to ignore (mask) multiple layers, each one represented by a single bit in the 32-bit integer.
In your code above, 9 (Collision) is a layer, NOT a mask. Raycast takes a layermask.
If you want to turn a layer name into a LayerMask, here’s the utility you want:
You can also go cheap and cheerful and rotate the bits yourself:
int layer = 9;
int layerMask = 1 << layer; // means take 1 and rotate it left by "layer" bit positions
That would cause layer 9 to be masked, ie,. selected to be hit by the raycast.
If you want to mask more than one layer, OR the bit-shifted results together:
int layerA = 9;
int layerB = 12;
int layerMaskCombined = (1 << layerA) | (1 << layerB);
To invert those masks (eg, take their opposite), use the tilde (~) (NOT A MINUS SIGN!) to flip every bit:
layerMask = ~layerMask; // tilde ~ is bitwise flip
That will return things marked with layer 9 or layer 12.
I’m not going to try to add in graphics to show off bitfields… there’s TONs of bitfield diagrams and tutorials all over the internet. It’s the basic fundamentals of math on a digital computer.
EDIT: corrected 1/5/2023 in response to @Bunny83 below… thanks man!