- Home /
Physics2D.CircleCast produces no result whenever a layer mask is specified
Here's the code:
RaycastHit2D hit;
int obscuringLayer = 8;
Vector2 dir = new Vector2 (1f, 0f);
BoxCollider2D boxCol = GetComponent <BoxCollider2D> ();
boxCol.enabled = false;
hit = Physics2D.CircleCast ((Vector2)transform.position, 0.4f, dir, 100f, obscuringLayer);
boxCol.enabled = true;
The problem is that the CircleCast will not hit ANYTHING on ANY layer. Strangely enough, this only happens if I specify the layer I want. if I put:
hit = Physics2D.CircleCast ((Vector2)transform.position, 0.4f, dir, 100f);
The CircleCast works perfectly (except, of course, that it hits objects I don't want it to hit). I've tried:
-Puting an integer OTHER than the mask for the layer I want it to hit (so I want it to hit layer 8 so I tried putting in layer 5 and it still wouldn't hit anything)
-Putting in a LayerMask that filters in ALL layers EXCEPT the one I want
-Putting in a LayerMask that only filters in the one I want
-Adding dynamic and static rigidbodies on the targets
-Testing the targets with isTrigger set to true and false
-Using Physics2D.LineCast with the same layer as an integer parameter
And I tried a few other things that I'm really wishing I wrote down right about now...
This always happens every time I try to raycast with a specific layer. I'm just trying to copy the style from Unity's Roguelike tutorial, they have code that looks like this:
//Disable the boxCollider so that linecast doesn't hit this object's own collider.
boxCollider.enabled = false;
//Cast a line from start point to end point checking collision on blockingLayer.
hit = Physics2D.Linecast (start, end, blockingLayer);
//Re-enable boxCollider after linecast
boxCollider.enabled = true;
I can't possibly imagine why on earth this code works perfectly fine but mine does absolutely nothing...
Answer by Bunny83 · May 05, 2018 at 01:31 AM
It's a layer mask, not a layer index. The layermask is a bitmask and since an integer value has 32 bits there are 32 layers. Explanations are here(UA) and here(YT).
The easiest way to specify a layer mask is to use Unity's LayerMask struct. When you create a public variable of that type you can select the layers you want in the inspector. The struct also as a couple of static methods to get the mask for a given layer name or the index for a given layer name.
If you like hardcoding the layers in your script the easiest way in code is to use the bitwise left shift operator. 1<<layerIndex
gives you a layer mask value with only the layer with the index "layerIndex". If you want multiple layers in your mask you have to combine the masks using the bitwise "or operator" |
like this:
int mask = (1<<10) | (1<<15); // layer 10 and 15
Note a value of 8 as layer mask is actually the binary number "0000 1000". So you actually specified layer index "3"
Your answer
Follow this Question
Related Questions
Physics2D.Linecast ignoring walls 1 Answer
Using Physics2D.IgnoreLayerCollision and LayerMask from Editor 1 Answer
Raycast2D not working with LayerMask 1 Answer
OverlapSphere doesn't detect colliders 1 Answer
Why is RaycastHit2d Not working 0 Answers