I find myself needing to create LayerMasks that are set to “Everything” fairly often in code. The easiest way I know of to do that is to manually set the value in the inspector. Is there an easy way to set a LayerMask to “Everything” in code? I would need it to work regardless of how many layers are in game so it doesn’t become broken if new layers are added.
I searched and experimented but could not find a solution. I figure there might be a simple solution I haven’t found. Does anyone know of such a solution? Alternatively, does anyone know for sure that such a solution does not exist?
Such a solution definitely exists, if you’re aware that a LayerMask is just a series of bits. I mean, obviously everything on your computer is, but the LayerMask is actually used as such: each bit represents a layer that is either on or off. Under the hood it’s represent as an int, and can be easily asssigned as such.
So the question is: what value of an int is all 1’s in binary? You could google that, and you’d get a number which you could plug in (you want the result of 2^32 - 1) and it’d work. But we can do this in a slightly cleaner way, using a bitwise NOT operator, which in C# is tilde (~) - which will literally reverse every bit in a value. So now the question is, what int value is all 0’s in binary?
Spoiler: it’s 0.
LayerMask someMask = ~0;
edit: beaten to it by someone who just had to type the answer. Well, now you know how that answer was arrived at.
For anyone wandering into here, bit operations are a powerful and very fast way to handle masks.
You can have
int layer1 = 1; // ..0001 as bits, 1 as a number
int layer2 = 1 << 2; // ..0010 as bits, 2 as a number
int layer3 = 1 << 3; // ..0100 as bits, 4 as a number
int layer4 = 1 << 4; // ..1000 as buts, 8 as a number
Then you compare them with AND, OR and other operators.
int res1 = layer1 & layer2 // ..0011 as bits, 3 as a number (2+1)
int res2 = layer1 & layer3 // ..0101 as bits, 5 as a number (4+1)
if (1 << gameObject.layer & layer1) > 0)
{
// do stuff
}
The above if-statement compares two layers to each other, and if they are the same (have the on-bit in the same place) it returns true because the resulting int has all bits on that are on in both. The fact that gameObject.layer is an int representing the number of the layer in the layer list complicates it a little, but not much. To get that as a bit, just do the 1 << layer operation to get it as bit representation.
You could also build preset masks with the OR operator:
public static class Layers
{
public const int Character = 1 << 6;
public const int Destructible = 1 << 7;
public const int Indestructible = 1 << 8;
public const int ProjectileHits = Character | Destructible | Indestructible;
}
Its a problem because LayerMask is an int, which is signed meaning you can’t normally just set bits without using negative values because the language syntax processor is checking to make sure you don’t make an error.
But, if you know what you are doing, you can just use an unchecked syntax that and it will let you set the bits, like so:
Honestly, only thing I wanted was LayerMask.All or something. Seriosuly, who looks up manual of Physics namespace when there’s already LayerMask class and GetMask or something are there? Especially, in the stuation where online manual constantly gives me " Sorry… that page seems to be missing!", duh!