Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 xEnOnn · Oct 11, 2012 at 02:36 AM · mouseposition

Getting GameObject's local mouse position relative to screen point

I have a GameObject of a plane in my scene. The GameObject is facing the camera upright, like in the screenshot below:

alt text

When I do a `Input.mousePosition`, I am getting the coordinates of the mouse on the screen. How can I get the mouse position that is local to the GameObject, but is still relative to the screen?

Because of how my scene is currently set up, I cannot use `GUITexture`, which has a handy `GetScreenRect()` for this purpose.

So for example, when I click on the red dot shown in the picture above, `Input.mousePosition` may give me `(450, 380, 0)`. This is the screen's point. My intention is to get a mouse position that is local to the GameObject. So in this example, suppose the GameObject has a width of `200px` and a height of `150px` on the screen. By clicking on the red dot position (the middle of the GameObject), I should get `(100, 75, 0)` as my mouse position.

How can I achieve this?

Thanks!

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

1 Reply

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

Answer by Muuskii · Oct 11, 2012 at 03:21 AM

Try constructing a Plane then use Camera.ScreenPointToRay to get a ray pointing along the mouse position and then use Plane.Raycast using that ray to get where the ray intersects the plane.

Make sure, when constructing the plane that it is in the same position as the GameObject and that it has the same orientation. Do this by setting the plane's normal to GameObject.Transform.Up

Edit: Your version is close but here is a few changes:

 float hitdistance = 0.0f;                        
         Ray ray = camera.ScreenPointtoRay(Input.mousePosition);  //You need to keep this for later
         Plane plane = new Plane(transform.up, transform.position);
         if( plane.Raycast(ray, out hitdistance) )   //Naturally this should never return false. . .but, whatever
         {
             Vector3 hitPoint = ray.GetPoint(hitdistance);
         }

You don't replace the GameObject with the plane. It's just serving as a sort of "collider" for the raycast to intersect with. Difference between Plane.Raycast and Physics.Raycast is that the former will only hit the plane. So you don't have to do any checks if a given point should be used or not.

Note: If this script is not attached to the GameObject in question; You will have to replace the transform mentioned in the code above, with the GameObject's transform.

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 xEnOnn · Oct 11, 2012 at 03:40 AM 0
Share

Do I do something like this:

Plane plane = new Plane();

plane.Raycast(camera.ScreenPointtoRay(Input.mousePosition));

And I have to replace my current GameObject shown the picture in the question with the plane?

avatar image Muuskii · Oct 11, 2012 at 04:06 AM 0
Share

I edited my answer to include some code.

avatar image xEnOnn · Oct 11, 2012 at 07:39 AM 0
Share

Ahh..I understand now. Thank you so much for the 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

10 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

Related Questions

Mouse aims at object ? 1 Answer

Get the length from the object i click on to the mouserelease.. how? 0 Answers

AddForce at mouse position only on table area 0 Answers

Best way to hold mouse cursor on control while activated? 0 Answers

How to make input.mousePosition Vector2 centered to middle of screen? 2 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