So in the docs here they describe using bitmasks to pass to RayCast functions in order to filter objects by layers
I got this code working just fine, but I don’t understand what this is or why its done like this. I’ve never used bitmasks before or seen this syntax.
private int shoeLayer = 1 << 8;
private int shoeWallLayer = 1 << 9;
What is going on there, what is that syntax? What is it doing exactly? I’ve searched google some but cant really find any clear concise explanation of whats going on…
And why is Unity using bitmasks for this? Couldn’t an array of bool do just the same?
A bitmask is just a series of bits where each bit represents a toggle (a mask). If a bit is on, then the mask which it represents is on. Otherwise, it’s off. Bitshifting is just a way of turning on a particular bit when the bitmask is currently uninitialized.
A bitmask is a single 32-bit variable. An array of bools would be an array of 32 variables, each one using 8 bits. So a bitmask is much more efficient.
If your looking for a array of bools you could use this: BitVector32 it acts like a bool array of 32 size but in the size of a int.
This type won’t show up in the inspector however, and instead of a array where you’d do array[0] or array[1] you instead do (array[1<<0] or array[1<<1]). but theres spots where its helpful