Raycast2D is hitting itself even when set to ignore own collider

So i’ve searched for solutions to this weird problem and come up with nothing. I thought i’d ask you all, because i’m sure i’m doing something wrong here.

Basically, i have several 2D gameobjects either moving up or down the screen. They all have colliders, and a script attached to affect behaviour. Below is the excerpt in question.

private Rigidbody2D enemyRigidBody = GetComponent<Rigidbody2D> ();
public Vector2 direction; //set to either Vector2.Up or Down on instantiation
public Vector2 move;
void Update () {
		RaycastHit2D hit = Physics2D.Raycast (transform.position, move, 3);
		Debug.DrawLine (transform.position, (Vector2)transform.position+move*2, Color.red);
		if (hit.collider != null && hit.collider.gameObject != this.gameObject) {
			GetComponent<SpriteRenderer> ().color = Color.white;
		} else if (hit.collider != null && hit.collider.gameObject == this.gameObject)
			GetComponent<SpriteRenderer> ().color = Color.green;
		else GetComponent<SpriteRenderer>().color = Color.black;
		move.y = direction.y;
		move.x = horizontalDeviation/10;
		enemyRigidBody.velocity= new Vector2 (move.x * speed, move.y * speed);

On instantiation, the Vector2 “direction” is set to either (0,1) or (0,-1). The conditional statements are set up so that:

  • while not detecting another collider, the sprite is black
  • while detecting another collider, the sprite is white
  • while detecting its own collider, the sprite is green.

Gameobjects with a direction of (0,1) work as intended, remaining black until i move my player object in front of them. However, those moving downwards (0,-1), just stay green. What makes it even weirder is that, if i set the RaycastHit2D parameters to (transform.position, -move, 3), the downward-movers don’t detect themselves, and work just fine in detecting objects behind them.

And yes, i’ve unchecked “Queries Start in Colliders” in Project Settings>Physics 2D.

If anyone could help me i’d be very grateful, as this has had me stumped for a while. And please let me know if you need any more info. Thank you!

I know this thread is ANCIENT but just in case someone still having this issue…

Go to Edit → Project Settings → Physics2D → UNCHECK Queries Start In Colliders.

Or use this code inside Start or Awake call:

Physics2D.queriesStartInColliders = false;

My solution was to turn off the collider before the raycast and turn it on at the end:

for (int i = 0; i < sectorCount; i++) 
{
locations *.GetComponent<CircleCollider2D>().enabled = false;*

RaycastHit2D hit = Physics2D.Raycast (locations .transform.position, Vector2.up);
.
.
stuff
.
.
locations .GetComponent().enabled = true;
}

Well I have exactly the same problem. Even though I unchecked Queries Start in Colliders in the Project Settings>Physics2D; I still had the Enemy GameObject detecting itself by Raycast2D.
I didnt want to use LayerMask to ignore Enemy GameObject colliders, because I want the Enemies to still be able to Raycast against each other.
I’ve noticed that using Concave polygon collider is the main cause of this problem. Modifing it to convex, using primitive box/circle collider or using compound convex collider solved this problem for me.
I think its most probably a bug.

If there’s anybody around who needs to fix that Issue, here’s a workaround.

    public RaycastHit2D GetFirstRaycastHit(Vector2 direction)
    {
        //Check "Queries Start in Colliders" in Edit > Project Settings > Physics2D
        RaycastHit2D[] hits = new RaycastHit2D[2];
        Physics2D.RaycastNonAlloc(transform.position, direction, hits);
        //hits[0] will always be the Collider2D you are casting from.
        return hits[1];
    }

I found myself in a weird spot and after quite some time, only today I found the reason why my collider2d was still being detected, even with Edit → Project Settings → Physics2D → Queries Start In Colliders being unchecked, and even while using a simple BoxCollider2D.

The final answer is simple: If you use the Edge Radius feature on your collider2d, it will be detected by a raycast! To fix it I just reset the edgeRadius value before the raycast and set it back right after.
Working as intended now.