I need to do a raycast on a specific layer. I thought I’d use gameObject.layer and build the mask myself, but I don’t get the same value as if I use a LayerMask and pick the layer manually. Ideally I’d prefer using the object’s layer than having to add another inspector member.
I couldn’t find any explanation for my results. According to the documentation, 1 << layer should work. Any idea? Thanks!
EDIT: Here’s some additional information as requested by, erm, FriskyDingo.
int raycastLayerMask = 1 << gameObject.layer;
Physics.Raycast(..., raycastLayerMask);
gameObject.layer is 15, which corresponds to the correct layer number in the layer settings. Using this method, 1 << 15 is 32768. However, using a LayerMask, the int value when I check layer 15 is 1024. 2^10 is 1024.
The thing is, the bit shifting results seem more logical to me, but the LayerMask seems to be right. My raycast hits the expected object when layerMask is 1024, but not 32768.
Where does that come from? I have no idea. There doesn’t seem to be any relation with the built-in layers either.
So there. I’m stumped!