Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by ludiq · Mar 06, 2015 at 02:50 AM · raycastphysics2dcollider2dcollider.raycast

How can I do a 2D raycast against one specific collider?

For 3D colliders ( Collider ), I can do:

 collider.Raycast(ray, out hit, rayLength)

But for 2D colliders ( Collider2D ), there is no Raycast method.

There's this angry feature request from more than a year ago, but no response.

I cannot use layer masks, because I cannot predict on which layer my collider will be.

(How) can I do it?

Comment
Add comment · Show 3
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 hexagonius · Mar 07, 2015 at 12:27 PM 0
Share

$$anonymous$$aybe with Collider2D.OverlapPoint, if frontal

avatar image Prosto_Lyubo · Mar 07, 2015 at 02:01 PM 0
Share

Can't You just treat it as normal collider? Collider2D col; col.collider.Raycast(...);

avatar image komodor · May 23, 2016 at 12:15 PM 0
Share

actually i am going to use $$anonymous$$Depth and maxDepth but it's ridiculous they haven't fixed it yet

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Glurth · Mar 07, 2015 at 02:35 PM

Not sure if you want your rays to be exclusively on a 2D surface, If so, perhaps using the Physics2D raycast functions will work for your situation?

http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

If you want to detect an intersection with a FACE, rather than a EDGE, then the Physics (3D) raycast, should work.

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Comment
Add comment · Show 1 · 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 komodor · May 23, 2016 at 12:18 PM 1
Share

Not sure if you bothered to read it. He needs 2D raycast from point A to point B and get if one particular Collider2D was hit and it's not possible right now with Physics2D or Collider2D.

Don't know if he can but i definitely can't switch to 3D physics.

avatar image
0

Answer by TheAnosmic · Nov 25, 2019 at 10:58 PM

Hope it will help others, I needed to check for intersection in the same scenario, I used this:

 collider.bounds.IntersectRay()
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 indolence · Mar 04, 2021 at 01:00 PM

I was trying to solve a similar problem on my project and ended up with this workaround solution. I'm not sure if it's the best, but it seems to get the job done.

The key here is that one of the overload methods for Physics2D.Raycast lets you output an array or list of results instead of just getting the first hit. Then you can check through your results list and find the one that matches the game object you're checking against.

          List<RaycastHit2D> resultsList = new List<RaycastHit2D>();
 
          Physics2D.Raycast(raycastStart, raycastDirection, contactFilter, resultsList, rayCheckDist);
 
          foreach (RaycastHit2D result in resultsList)
          {
             Debug.Log(result.collider.gameObject);
             if (result.collider.gameObject == gameObjectToCheckFor)
                {whatever you want to happen here}
          }



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

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

6 People are following this question.

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

Related Questions

why Raycast2D doesn't work properly? 0 Answers

Physics 2d raycaster from EventSystems for overlapped colliders 0 Answers

RaycastHit2D problem with PolygonCollider2D 2 Answers

OnCollisionEnter fires, but OnCollisionExit does not 1 Answer

Is this a correct way to have multiple different compound colliders in one gameobject? 1 Answer


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