Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 EAlbers · Mar 07, 2014 at 05:43 PM · raycastcollidermousehit

[2D] Raycast to mouse cursor = offset on Y axis

Hello everyone,

I am trying to learn using raycasts for checking if someone clicks on a 2D sprite object with the mouse and I stumbled upon a really weird little thing: I need to click a certain distance below a clickable sprite in order to click on it.

Say, if you have a rectangle sprite with a 2D box collider, clicking on the rectangle image doesn't yield a hit. Moving the mouse cursor a small distance below the rectangle and clicking in 'empty air' reacts as if you clicked on the collider object. So somehow, the 'hit-detection' between the mouse cursor and the clickable sprite has become shifted on the Y-axis.

I have absolutely no clue how this is possible. And I checked to see if the box collider of the rectangle happens to be offset on the sprite image, but it isn't. Does anyone have a clue on what might be going on? I did try searching for earlier threads on this but couldn't really find anything, or only found problems with no answers.

Could someone explain why this is happening... and how to solve it? My unity is version 4.3.3f1, if that has something to do with it. I'll be trying to find a solution... but this really got me stuck.

The code I use:

 void Update ()
     {
         
         if (Input.GetMouseButtonDown (0)) {
             RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
 
             if (hit.collider != null) {
                 print ("object clicked: " + hit.collider);
                 print ("object tag: " + hit.collider.tag);
             
             }
 
             if(hit.collider.CompareTag("Exiter")) {
                 print ("Exit the game please!");
                 Application.Quit ();
             }
         
         }
 
     }
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 robertbu · Mar 07, 2014 at 05:51 PM 0
Share

I have a guess. This calculation:

 Camera.main.ScreenToWorldPoint (Input.mousePosition)

...will only work if the camera is Orthographic. It will not work correctly for a Perspective camera. If you are using a perspective camera, you need to pass a distance in front of the camera as the 'z' component of the vector you pass to ScreenToWorldPoint(). And even this will only work if the rotation of your camera is (0,0,0).

avatar image EAlbers · Mar 07, 2014 at 08:00 PM 0
Share

I checked the main camera in the inspector windows (since the main camera is being used for this and there are no other cameras). The position and rotation transforms are all set to 0 and the projection is set to Orthographic, not Perspective...

The depth setting in the camera settings was -1, but changing that doesn't seem to make a difference.

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Please help me with Raycast 1 Answer

Problem with LineCast Bullet 1 Answer

checking colliders from multiple raycast 1 Answer

The Child Is Not Hovered When Passing The Parent's Collider 0 Answers

How to limit a ray to go downwards to infinity, how can I make it down only 1 float. 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