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
0
Question by Danisuper · Apr 12, 2016 at 04:41 PM · 2draycastraycastingclickraycasthit2d

2D click on object with a raycast not working

Hello all,I need to check the click on a 2D gameobject. I've tried this:

 if (Input.GetMouseButtonDown(0))
         {
             Ray2D ray = new Ray2D(Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y)), Vector2.zero);
             RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
             if (hit.collider!=null)
             {
                 Debug.Log(hit.collider.gameObject.name);
             }
         }

but it doesn't work... So I tried to pass a 3d ray on a Physics2D.Raycast

  if (Input.GetMouseButtonDown(0))
         {
             Ray b=Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit2D hit = Physics2D.Raycast(b.origin, b.direction);
             if (hit.collider!=null)
             {
                 Debug.Log(hit.collider.gameObject.name);
             }
         }

But it doesn't work too,I've also tried to manually set a distance and a direction in the ray's declaration and in the raycast's physics2d.raycast method but it didn't change nothing. This piece of code is in the Update method but I tried it also on the FixedUpdate but it was the same thing. The only time when it detected some colliders is when i set the direction to -vector2.up but it detects only the terrain gameobjects.I tried to hack it by setting the origin to a higher point and reducing the distance but it didn't work well,it doesn't detect all the colliders. I searched on the internet almost all the arguments about detecting a click in a 2d context,I found the OnMouseDown() method but I prefer using the raycasting,to learn it. I've also seen that a lot of people use these 2 ways to detect a click but it doesn't work to me. What's wrong? Thank you in advance for your help :D

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Danisuper · Apr 13, 2016 at 07:02 PM

Found a solution to check what object I clicked on,I used Physics2D.OverlapPoint() here the complete code:

     GameObject ClickedGameobject()
      {
         GameObject clicked;
         Collider2D clicked_collider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
         clicked = clicked_collider.gameObject;  
         return clicked;
      }
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 aditya · Apr 13, 2016 at 05:18 AM

why are you using COMPLICATED rays instead of SIMPLE SpriteRenderer.bounds.Contains

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 Guhanesh · Apr 13, 2016 at 05:46 AM 0
Share

you should add this as comment ins$$anonymous$$d as a Answer.

avatar image Danisuper · Apr 13, 2016 at 06:28 PM 0
Share

it may be a good input,I tried working around on it,but the problem now is how do I know wich object belongs the spriterenderer?

            GameObject go;
             SpriteRenderer go_renderer=go.GetComponent<SpriteRenderer>();
             Vector3 mouse_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             if(go_renderer.bounds.Contains(mouse_pos))
             {
                 //do stuff
             }

I mean,how to assign go? I must check if I click on a monster/object,so this code isn't going to be on the object with the spriterenderer in the code that check if the mouse in in bounds,that is what is blocking me now,a solution may be putting all gameobjects with a tag in an array and foreach object in the array checking the bounds but I think it would be very inefficent because in the game there will be a lot of calls to this. I think raycasts are the best solution but looks like the code in my question isn't working lol

avatar image aditya Danisuper · Apr 15, 2016 at 05:29 AM 0
Share

just make go_renderer no go needed and simply drag and drop the gameObject which have your sprite on it and the script will get your spriterenderer, just make the variable public, just make sure that go_renderer is of type Spriterenderer

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D Raycast not working 1 Answer

Is there a better way to find where to start my raycast? 2 Answers

My Raycasts seem to sometimes miss 0 Answers

onGround raycasts return onGround is true while colliding with platform sides 0 Answers

Layer Mask on raycast2d not working? 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