Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
15
Question by eForger · Oct 24, 2015 at 01:40 PM · cameraraycastcollider2drayscreenpointtoray

Get 2D Collider with 3D Ray

I saw a lot of answers about this question but none makes the job like I wanted.

Examples :

Select 2D Object By Mouse

Raycasting to find mouseclick on Object in unity 2d games

Raycast on a 2D Collider

Unity 2D Raycast from mouse to screen

How to detect a touch on Box Collider 2d in Unity 4.3

And maybe others...

So, I was searching to for a method to catch 2D colliders raycasting a 3D ray from the camera, knowing it was possible but didn't remember how.

Searching solutions on the web, I remembered that I had already do something great in a last project. Founded it in my files and re-done it in my new project, I want to share you this method.

The tip is to use the GetRayIntersection method in Physics2D which let us cast an arbitrary 3D ray to catch a 2D collider : look here.

There is its little sister : here.

So you can do something like this :

 private void GetTargettedGO ( Vector2 screenPosition )
 {
         Ray ray = Camera.main.ScreenPointToRay ( screenPosition );
     
         RaycastHit2D hit2D = Physics2D.GetRayIntersection ( ray );
     
         if ( hit2D.collider != null )
             Debug.Log ( hit2D.collider.name );
  }


And that's all !

Of course this method doesn't return anything for now, but from here, it's easy to do what you want ! ;)

Comment
Add comment · Show 1
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 aklgupta · Mar 06, 2019 at 04:02 PM 0
Share

Awesome... I was facing a similar issue, and just didn't know how to make a 3D ray interact with a 2D collider. Thanks a lot! It solved my issue.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by windship · Apr 01, 2020 at 10:52 AM

This problem made me almost crazy but your solution cleared my issue also. Thank you very much! Bless you!!!

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 eForger · Apr 01, 2020 at 11:47 AM 0
Share

Hey ! Happy to see that this solution is still valid with new Unity's versions ! :)

And happy to help ya ! =)

avatar image
0

Answer by abssuper20 · Sep 09, 2020 at 06:47 AM

Awesome!! Works perfectly. Thanks a lot!!

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 eForger · Sep 09, 2020 at 04:44 PM 0
Share

Always happy to be able to help :)

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

40 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

Related Questions

Why is the camera.screenpointtoray off? 1 Answer

Follow mouse cursor (with Y = 0)? 1 Answer

mouse position on terrain 1 Answer

How to use ScreenPointToRay for orthographic cameras 1 Answer

is it possible to have raycasts to cover the whole screen in one Update() time? 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