Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by rogmidd · Apr 16, 2016 at 08:11 PM · unity 5physics2draycasthit2dphysics2d.raycast

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rogmidd · Apr 17, 2016 at 06:34 AM 0
Share

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.

avatar image MelvMay ♦♦ rogmidd · Apr 17, 2016 at 08:29 AM 0
Share

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.

5 Replies

· Add your reply
  • Sort: 
avatar image
15

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;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image The_MrX_ · Oct 13, 2019 at 05:12 PM 1
Share

Ohh my god, why is that turned on by Default. Facepalm...

Thank you!

avatar image dead_byte_dawn · Apr 02, 2021 at 05:14 PM 0
Share

Can confirm.... this was in Unity 2020.3.2f1 as well.... Le sigh...

avatar image s10195618 · Jul 12, 2021 at 01:37 AM 0
Share

OMG you saved my life. Yesterday I spent the whole afternoon trying to solve this...

avatar image Ulfnir · Sep 08, 2021 at 11:25 PM 0
Share

2021 and still saving lives with this, thanks

avatar image
1

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];
     }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FlaSh-G · Jun 06 at 09:04 PM 0
Share

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.

avatar image MelvMay ♦♦ FlaSh-G · 6 days ago 0
Share

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.

avatar image FlaSh-G MelvMay ♦♦ · 6 days ago 1
Share

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".

Show more comments
avatar image
1

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image abees81 · May 21, 2016 at 07:42 PM 0
Share

Forgot to mention that am using Unity 5.3.4 the free version. Hope this will help.

avatar image RodrigoSeVeN · Mar 19, 2020 at 01:34 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MelvMay ♦♦ · Mar 19, 2020 at 03:39 PM 1
Share

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.

avatar image RodrigoSeVeN MelvMay ♦♦ · 5 days ago 1
Share

Very nice. I failed to report it as a bug, but luckly someone else did it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I think my raycast detects destroyed gameobjects 0 Answers

Physics2D.Raycast producing bizarre results. 0 Answers

Problems using RaycastHit2D.collider, detection only on the rays origin 1 Answer

Checking for Raycast distances not working as expected. 0 Answers

Adding some offset value for RayCast 2D but what is the distance unit? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges