• 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
4
Question by bsbimalbose · Dec 14, 2013 at 05:33 PM · 2draycastcollider

Raycast on a 2D Collider

I want to raycast from the camera to the mouse location for my @D Game to identify which Object was clicked.

     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

     RaycastHit hit;
     if(Physics.Raycast(ray,out hit))
     {
         isHit = false;
         Destroy(GameObject.Find(hit.collider.gameObject.name));

     }


Raycasting on 2D Colliders doesn't seem to work.

Comment
Add comment
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

6 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Dec 14, 2013 at 08:12 PM

As you have found, 3D Raycasting (Physics.Raycast()) on a 2D colliders does not work. You have a couple of choices. First Monobehaviour.OnMouse* functions do work, so you can put a script directly on the object. If you want to Raycast(), then an alternate solution is to put a 3D collider on an empty child game object and size the collider as appropriate to your sprite. You can use the Transform.parent to access the sprite.

Comment
Add comment · Show 6 · 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 bsbimalbose · Dec 15, 2013 at 03:08 PM 0
Share

Thanks I added 3D colliders as childern to the Sprite Objects and it works fine

avatar image robertbu · Feb 15, 2014 at 10:24 PM 4
Share

Note my answer here is old and while workable, is not the best answer. The Physics2D.Raycast() using a Vector2.zero for direction works fine. In addition, while I've not seen it in an answer, Physics2D.OverlapPoint() might even be cleaner.

avatar image M0no robertbu · Aug 05, 2016 at 02:51 PM 2
Share

please edit your answer above to include this info

avatar image AntFitch · Feb 16, 2014 at 12:40 AM 0
Share

This is a nice snippet if Physics2D.OverlapPointAll() is used: http://forum.unity3d.com/threads/211708-Unity-2D-Raycast-from-mouse-to-screen

avatar image luniac · Nov 06, 2014 at 03:05 PM 0
Share

Is overlap point possibly cleaner than this? as long as ur looking for the first touch that is.

 var worldTouch = Camera.main.ScreenToWorldPoint(touch.Position);
 var hit : RaycastHit2D;
 hit = Physics2D.Raycast (Vector2(worldTouch.x,worldTouch.y),Vector2.zero, $$anonymous$$athf.Infinity);
avatar image ProbePLayer · Feb 05, 2018 at 10:02 PM 0
Share

There is a third solution which is by far the most accurate for the question, and it is described in the @sushanta1991 's answer. Namely, you use Physics2D.Raycast function but with a twist. You raycast from the mouse position to the mouse position, which gives you any object under the mouse position :)

avatar image
14

Answer by MarcosLC · Dec 14, 2013 at 06:49 PM

For 2D Colliders you need use Physics2D and RaycastHit2D instead of Physics.

 void Update(){
         
         RaycastHit2D hit = Physics2D.Raycast(cameraPosition, mousePosition, distance (optional));
         
         if(hit != null && hit.collider != null){
             isHit = false;
             Destroy(GameObject.Find(hit.collider.gameObject.name));
         }
     }

Use it with a Vector2 mousePosition or if your game is 3D use 3D colliders instead of 2D.

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 AntFitch · Feb 15, 2014 at 10:20 PM 6
Share

To clarify:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
avatar image
7

Answer by sushanta1991 · Jan 01, 2015 at 11:46 AM

You can try this solution given in this link

Comment
Add comment · Show 3 · 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 cmos · Mar 15, 2016 at 08:26 PM 1
Share

This is the only solution that worked for me in this thread.

avatar image cmos cmos · Mar 15, 2016 at 08:28 PM 3
Share
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = 10;
 
         Vector3 screenPos = Camera.main.ScreenToWorldPoint(mousePos);
 
         RaycastHit2D hit = Physics2D.Raycast(screenPos,Vector2.zero);
 
         if(hit)
         {
             print (hit.collider.name);
         }
avatar image ProbePLayer · Feb 05, 2018 at 10:03 PM 0
Share

Yes this answer is clearly the best here.

avatar image
3

Answer by _MGB_ · Dec 14, 2013 at 05:55 PM

If you're using the new 2d stuff you'll need to use the Physics2D functions.

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
3

Answer by It3ration · Aug 19, 2014 at 09:36 PM

Use the following to cast 3D rays against 2D colliders:

 Physics2D.GetRayIntersection(...)
 Physics2D.GetRayIntersectionAll(...)
Comment
Add comment · Show 3 · 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 Utoxin · Mar 02, 2018 at 03:09 PM 0
Share

This is the most useful answer here. It allows you to use 3D raycasts from the camera to check for 2D colliders. Very helpful!

avatar image sandolkakos · Aug 23, 2018 at 07:59 PM 0
Share

I didn't know about this. Thank you so much, @It3ration . Now I can get Collider2D from Cameras with Perspective projection.

avatar image Ryun · Jan 08 at 04:28 PM 0
Share

I did the login only to upvote this answer, thanks dude, this solved my problem, upvote this guy

  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 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

Related Questions

2D melee Combat with SphereCastAll Problem 1 Answer

Physics raycast hit offset from where it should be 1 Answer

Questions about movement on uneven/slopped terrain and terrain coliders 0 Answers

Detecting if the object is hit by LineRenderer 1 Answer

Detect only UI button click 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges