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…