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 RhinoPhyre · Mar 05, 2018 at 12:21 AM · 2draycastmousepositionraycasthit2d

2D - finding object at mouse position

I am building a 2D platformer (sort of). Default Unity setup, looking down the Z axis. I need to do two different things with detecting the mouse position.

1) Player can interact with the item he's pointing at with the mouse (both in world, and in UI). 2) A UI feature needs to show a tooltip when the mouse is over certain items.

All my objects in game world are at z = 0. My canvas was at z = -5, but I've moved that to 0 too now. My camera is at z = -10. My camera follows the player in X and Y axes.

I have tried three approaches to this:

 Vector3 mousePosition = cam.ScreenToWorldPoint(Input.mousePosition);
 mousePosition.z = -10;
 RaycastHit2D hit2 = Physics2D.Raycast(cam.transform.position, mousePosition - cam.transform.position, 500, mouseInteract);
 if (hit2.collider != null)
 {
     mouseObject = hit2.transform;
 } else
 {
     mouseObject = null;
 }

The "2D" approach assumes all co-ordinates are at z=0, so instead of casting a ray from the camera, it casts from the player (which is at the same X/Y, but at z=0). This means the player can only interact with the items closest to him (which might work, but it's not what I'm trying to do right now). It also never collides with anything in the UI layer. (the layerMask does include the UI layer).

 Ray ray = cam.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, 100))
         {
             //Debug.Log(hit.transform.gameObject.name);
             mouseObject2 = hit.transform;
         } else
         {
             mouseObject2 = null;
         }
 
  RaycastHit hit3;
         if (Physics.Raycast(cam.transform.position, mousePosition - cam.transform.position, out hit3, 500, mouseInteract))
         {
             mouseObject3 = hit3.transform;
         } else
         {
             mouseObject3 = null;
         }

These two different 3D approaches never detect anything. Neither the UI nor the gameObjects in world.

I'm not sure if there's a way to use the 2D physics to do this, as Unity is still 3D, as much as it would like to think it isn't when you're in 2D mode. But I am totally at a loss as to why the 3D raycasts aren't picking up anything at all.

I have tried setting "mousePosition.z = -10;" to 0, 10, -10, 5, -5. None of those changed anything.

I'm sure someone is going to point out the obvious problem in less time than it took me to type all this...

Thanks in advance!

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 RhinoPhyre · Mar 05, 2018 at 03:32 AM 0
Share

Well, I have made some progress. Swapping the BoxCollider2D on the 2D in-game objects for a BoxCollider allows them to be picked up by the 3D ray version of the logic. However, I also had to swap the RigidBody2D for a RigidBody too, and that means the objects now fall off the back of the game world, as they are free to move on the Z access.

Also, my player object no longer collides with these objects, as the player is only 2D, not 3D.

So I think this has to be a 2D solution, but I have no idea how to make that work. Please someone help! :)

avatar image RhinoPhyre RhinoPhyre · Mar 05, 2018 at 03:34 AM 0
Share

I have made more progress. I realized by reversing the 2D raycast, I get the collision closest to the mouse, not the player. And by shortening the range, I don't get a collision if I move off the object. So I think my in-game objects issue is solved.

But I still have no idea how to get a collision with the UI elements. If I give them a Box Collider, I won't be able to walk through them, and that's definitely not the plan!

Still need help, just less of it than I thought!

2 Replies

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

Answer by RhinoPhyre · Mar 06, 2018 at 01:35 AM

Well, I solved it.

My 2D raycast solution was closest to a working solution. I had to reverse the Vector3s, so it was originating at the mouse and casting towards the player. This ensured it would hit the object the mouse was over first, and not the one closest to the player along that line. But when the mouse was over nothing, it would still hit the one closest to the mouse in that direction. So I had to shorten the range as well. Now that part works 100%

 RaycastHit2D hit2 = Physics2D.Raycast(mousePosition, cam.transform.position - mousePosition , 0,001, mouseInteract);

The UI I had to do totally differently. I had to use Rect.Contains(Input.MousePosition), combined with an annoying variety of conversion functions to get the co-ordinates in the same systems. Neither 2D nor 3D raycasts would hit the UI elements.

This was a frustrating day with very little progress over all! :P

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 SadSwede · Mar 05, 2018 at 02:29 AM

Try Physics2D.Raycast

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 RhinoPhyre · Mar 05, 2018 at 02:41 AM 0
Share

What should I do different from the first example in the question?

 RaycastHit2D hit2 = Physics2D.Raycast(cam.transform.position, mousePosition - cam.transform.position, 500, mouseInteract);

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

181 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Why is my raycast2D offset from the mouse position? 0 Answers

2D Raycast over multiple objects not working? 1 Answer

My Raycasts seem to sometimes miss 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