Harvii
September 21, 2022, 12:58am
1
How can I make it so the raycast can detect 2 layers instead of 1?
public LayerMask collisionLayerMask;
RaycastHit2D raycastHit = Physics2D.BoxCast(bc2D.bounds.center, bc2D.bounds.size, 0f, Vector2.down, extraHeightTest + extraHeightTest, collisionLayerMask);
The difference between Layers vs LayerMasks:
The layer is a number from 0 to 31. It corresponds to each of those named layers.
A layer MASK is a 32-bit bitfield that says which of these you want to ignore. This enables you to ignore (mask) multiple layers, each one represented by a single bit in the 32-bit integer.
In your code above, 9 (Collision) is a layer, NOT a mask. Raycast takes a layermask.
If you want to turn a layer name into a LayerMask, here’s the utility you want:
You can also go cheap and cheerful and rotate the bits yo…
“There are 10 types of people in this world: those who understand binary, and those who don’t.”
In your inspector, for the collisionLayerMask var your created, you can selected multiple Layers in that dropdown it’ll then only apply to those Layers.
3 Likes