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'm not sure if this helps, but i was using a Polygon Collider 2D. I just switched to a Box Collider 2D and the problem has vanished. It'd be great to be able to use the Polygon Collider 2D, so if anyone has any wisdom, it'd still be very much appreciated.
Queries start in collider doesn't stop it hitting 'self', you use layer filters for that. Behind the scenes, the Box and Polygon colliders produce the same thing i.e. polygons (Box is just a convenience) so it's more likely you're just hitting yourself with a more complex polygon via the polygoncollider rather than the boxcollider.
Answer by EmreB99 · Feb 28, 2019 at 09:44 PM
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;
Ohh my god, why is that turned on by Default. Facepalm...
Thank you!
Can confirm.... this was in Unity 2020.3.2f1 as well.... Le sigh...
OMG you saved my life. Yesterday I spent the whole afternoon trying to solve this...
Answer by dCalle · Feb 20, 2017 at 05:46 PM
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 know this is old, but please note that this particular way of using RaycastNonAlloc
produces the same amount of garbage as RaycastAll
because an array is allocated for it with each method call.
I wrote the 2D physics in Unity and I can tell you that this isn't true at all.
The only allocation that RaycastAll does it create an array and returns it to you, this is why it's bad.
RaycastNonAlloc doesn't create an array as can clearly be seen in the API reference , it expects you to pass an array or list and reuse it. Yes you'll still have the problem if you don't reuse the array/list because that would be an odd thing to do.
Reuse the array.
Sorry, but what??
RaycastAll creates an array. This method creates an array. And it is not reused. I didn't say "RaycastNonAlloc produces the same amount of garbage", I said "this use of RaycastNonAlloc produces the same amount of garbage".
Answer by Tsilliev · Mar 19, 2018 at 12:28 PM
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 [i].GetComponent<CircleCollider2D>().enabled = false;
RaycastHit2D hit = Physics2D.Raycast (locations [i].transform.position, Vector2.up);
.
.
stuff
.
.
locations [i].GetComponent<CircleCollider2D>().enabled = true;
}
Answer by abees81 · May 21, 2016 at 07:39 PM
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.
Forgot to mention that am using Unity 5.3.4 the free version. Hope this will help.
Your answer helped me realize that the edge radius of a BoxCollider2D could be considered as a separate collider, and I think internally it is, and that causes it to be detected by a raycast.
Answer by RodrigoSeVeN · Mar 19, 2020 at 01:30 PM
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.
FYI: This was reported and fixed here: https://issuetracker.unity3d.com/issues/boxcollider2d-with-an-edgeradius-0-results-in-physics2d-dot-queriesstartincolliders-being-ignored-for-queries
It's landed in 2020.1b2 (not out yet) and is pending landing in 2019.3 With this fix, raycasting from the interior of a BoxCollider2D with an EdgeRadius > 0 no longer detects the edges.
Very nice. I failed to report it as a bug, but luckly someone else did it.
Your answer
Follow this Question
Related Questions
I think my raycast detects destroyed gameobjects 0 Answers
Character suddenly stop moving. 0 Answers
Overlap Circle and Layer mask 0 Answers
Raycast2D from camera does not hit objects 1 Answer
Strange behavior when applying forces. 0 Answers