I have searched a bit on the forum for how to see if a layermasks layer is enabled, although I can only find how you create a layermask.
The reason is that when building a custom inspector you can’t create a LayerMask field, so I’m trying to create one myself.
It’s not quite as simple as that, unfortunately. The layer mask is an integer, and you need to test just a single bit. You need to do something like:-
if (((layermask >> layer) 1) == 1) {
...
}
What this does is shift the bit that you want to test into the rightmost bit (ie, the digit that represents the value of 1). The bitwise-and operator then sets all bits except this one to zero. The value in the integer is then the value of the specific bit you want to test - it will be zero if false and one if true.
Those are both “harder” than this more common method (and IMO also less intuitive):
if (layerMask & (1 << layer) != 0)
{
Debug.Log("I belong in this mask.");
}
It works because the 1 << layer creates an int with a single bit shifted over layer number times (layer 1 gives you 0001, layer 2 gives you 0010, layer 3 gives you 0100, etc). Then by using the bitwise and operator (&) it will return any bits that exist in both integers. So, it gives you a 0 if no bits match.
Not just in Unity 2020 ^^. Have a look at the Operators Precedence in C#. So the shift operator has the highest priority of the 3 operators we used. Therefore the brackets he used are pointless since the shift is done first anyways. However the issue is that the comparison operator != has a higher priority than the bitwise AND operator &. So when we break down his code it actually tries to do this
int mask = 1 << layer;
bool tmp = mask != 0;
bool result = layerMask & tmp;
if(result)
hopefully you see the result line makes no sense since we try to use the bitwise AND between a layermask and a boolean value. What you have to do is using brackets like this
if ((layerMask & 1 << layer) != 0)
This will result in this code
int mask = 1 << layer;
int tmp = layermask & mask ;
bool result = tmp != 0;
if(result)