How to create a user layer that ignore Raycast?

How to create a user layer that ignore Raycast?

testure’s example casts only against this one layer (8) and ignores all others.
If you want to cast against all but you want to exclude layer 8 do:

var layerMask = ~(1 << 8);
if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask))
    [...]

Just to clear things up. The Layermask is a bit-mask. Every bit in the integer represents one layer (an int have 32 bits therefore we have 32 layers). The first 8 layers (0 - 7) are predefined. The last 24 layers (8 - 31) are free to use.

Some examples:

1 << 8
0000 0000 0000 0000 0000 0001 0000 0000 (bin) or        256 (dec) or 0x00000100 (hex)
~(1 << 8)
1111 1111 1111 1111 1111 1110 1111 1111 (bin) or 4294967039 (dec) or 0xFFFFFEFF (hex)

There are some constants defined in Physics that can be used to add or remove your own layer.

Physics.kAllLayers            defaults to 0xFFFFFFFF, as signed integer = -1
Physics.kDefaultRaycastLayers defaults to 0xFFFFFFFB, as signed integer = -5
Physics.kIgnoreRaycastLayer   defaults to 0x00000004, as signed integer =  4

To combine those “numbers” you can use the binary operators (|, &, ~, |=, &=, <<, >>, <<=, >>=)

//Use the default mask but exclude my user layer 8
var layerMask = Physics.kDefaultRaycastLayers & ~(1 << 8);
// which results in:
// 1111 1111 1111 1111 1111 1110 1111 1011  (0xFFFFFEFB)

For more information on bitwise operators take a look at wikipedia

I’m basically going to say what testure said with an added detail or two.

First, create your custom layer. See this page for that. Then, it will say “User Layer” + a number. Remember the number, I’ll explain that in a moment. Example: “User Layer 8”. The number 8 is important. Then create a layermask. This post here does a very thorough job explaining how layermasks work so I will give a quick example.

var layermask : int = 1 << 8;
//This is where the 8 comes back into it.
//This operation is called bit shifting.  it shifts the binary representation of the number on the right 8 spaces to the left.

So in this example, first convert 1 to binary. There would be 32 zeros since its a 32bit int, but I’m just going to show some of them:

 1 in decimal becomes
 00 0000 0001 in binary

then we shift it 8 bits to the left:

  01 0000 0000

Above is the most common way of creating layermasks, but just for examples sake, you can also create a layermask using any int you want.

 example 
 var layermask : int = 5;

converted to its bit representation, that would look something like:

  00 0000 0101

which would also be a valid layermask. Either way would work, most of the time I see people use the first method because its more clear what your intentions are.

When you pass a layer mask to a function such as raycast, Unity looks at the bit representation. Wherever there is a 1, the Raycast will check collisions with that layer. Where there is a 0, it will ignore collisions.

Physics.Raycast (Vector3.zero, Vector3.forward, 1000, layermask)

// bit shift the index of the layer to get a bit mask. In this case our custom layer is index 8.
var layerMask = 1 << 8;
// Does the ray intersect any objects which are in the player layer.
if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask))
print (“The ray hit the player”);