Howdy folks,
I’ve tried reading raycast & bitmasking documentation, and I’ve searched around the forums. However, I cannot get my head around this issue.
I’m trying to hit objects in a certain layer (layer 10) using Physics.Raycasts. However, the ignore-certain-layers function of raycasts confuses me. This is my current code:
if (Physics.Raycast(ghostCamera.transform.position, ghostCamera.transform.forward, out hit, nearRadius, ~(1 << 10)))
{
Debug.Log(hit.transform.name);
}
As I understand it, “~(1 << 10)” should mean:
Ignore every layer that isn’t layer 10.
However, it does not! It actually hits the player model (In which the camera is positioned) first. So I created a new layer for the player model, layer 11, and tried again. It didn’t work!
I messed around and found this solution:
if (Physics.Raycast(ghostCamera.transform.position, ghostCamera.transform.forward, out hit, nearRadius, ~(1 << 11 | ~(1 << 10)))
{
Debug.Log(hit.transform.name);
}
Which should work as intended, but bothers me to no end. I interpret ~(1 << 11 | ~(1 << 10)) to be:
Ignore if either: layer 10 or not layer 11.
I’m really confused as to why this works. It seems so illogical to me.
Can anyone help me with this?
Thanks!
Kind regards.