Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
2
Question by kag359six · Nov 24, 2012 at 05:04 PM · mousescreendragcoordinatesworld

getting world coordinates of mouse position

There are many questions like this, but none resolve my current problem. I'm trying to use the mouse position to set the position of the object I'm clicking in. For example, if I want to drag a box around on the x and y axis. When I use camera.ScreenToWorldPoint it keeps giving me the position of the camera rather than the mouse.

         if(Input.GetMouseButton(0)) {
             
             if(Physics.Raycast(ray,out hit,100f)) {
                     
                 Vector3 mouseWorld = camera.ScreenToWorldPoint(Input.mousePosition);
                 //Debug.Log (mouseWorld);
                 
                 if(hit.collider.gameObject.tag == "dragable") {
                     
                     hit.transform.position = mouseWorld;
                         
                 }
     
             }
         
         }
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
2

Answer by TheDarkVoid · Nov 24, 2012 at 05:07 PM

you need to use Camera.ScreenPointToRay and use the hit info to get the object's position.

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 kag359six · Nov 24, 2012 at 06:10 PM 0
Share

I want to SET the position of the object, sorry if that was unclear. I can get the hit info no problem, but I'm trying to set it equal to the mouse position in world coordinates.

avatar image TheDarkVoid · Nov 24, 2012 at 07:18 PM 0
Share

Input.mousePosition doesn't give a screen point, you will have to subtract the y from screen.height to get the screen point

avatar image kag359six · Nov 24, 2012 at 08:26 PM 0
Share

what do you mean? Doesn't it return the x and y pixel coordinates?

avatar image TheDarkVoid · Nov 24, 2012 at 09:06 PM 0
Share

it return the x,y, but ti does so where y = 0 is the bottom of the screen, while screen point y=0 is the top

avatar image kag359six · Nov 25, 2012 at 01:58 AM 0
Share

So how does ScreenPointToRay(Input.mousePosition) work and ScreenToWorldPoint(Input.mousePosition) doesn't?

Show more comments
avatar image
0

Answer by nenorse · Nov 16, 2013 at 10:07 PM

Anycase anyone is interested, I also had the same problem. I managed to resolve it by making sure of three things

  1. The Input.MousePosition.x is reversed (Screen.Width)

  2. The Input.MousePosition.y is reveresed (Screen.Height)

  3. The Z of the Vector is based from the camera position and in my case the z depth of the object that I was looking at (as they were all the same size).

Or..

Vector3 inputPosition = Input.mousePosition; Vector3 ray = Camera.mainCamera.ScreenToWorldPoint(new Vector3(Screen.width - inputPosition.x, Screen.height - inputPosition.y, Camera.main.transform.position.z - 2f));

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

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

12 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

Related Questions

Transform's position wherever the mouse is pointing. 2 Answers

world to screen coordinates conversion 1 Answer

How to get mouse position in World Space when in Editor Mode 1 Answer

Unity2d(mobile) Drag level screen left/right 2 Answers

"Launch" a rigidbody 3 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