Raycasting layermask

Just have a question about layermasks, Im trying to linecast from a turret the target. Im using
Physics.Linecast(pos1,pos2,~8) im trying to cast onto all but one layer am I doing it right?

thanks

Sort of.

The third parameter is supposed to be a layermask, not an integer (reference here for a good illustration).

The proper way to go about it (assuming you want to ignore only layer 8) would be:

Physics.Linecast(pos1,pos2,~(1<<8));

You had the proper syntax for the inversion (the ~) but, working with layermasks, it’s essentially one byte 32 bits of data that you need to supply.

Working with your original statement (using the integer 8), it would translate into 00001000 in base 2. So, while ignoring layer 4 and casting to all others does ignore one layer, it just happens to work out that way because of the binary representation of the int 8. Also, it’s ignoring layer 4, not layer 8.

As I was typing, I found an essentially definitive guide to layermasks in an older question so check that out too.