• 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
1
Question by aidenkael · Oct 31, 2013 at 12:08 PM · c#mouse

Getting the Vector2 of the mouse position.

How can I get the Vector2 of the mouse position in game? Is there a way I can convert the pixel position from Input.mousePosition.x/y?

To be more clear, I need to world position, not the screen position

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
6
Best Answer

Answer by Dave-Carlile · Oct 31, 2013 at 12:09 PM

 Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Comment
Add comment · Show 10 · 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 aidenkael · Oct 31, 2013 at 12:11 PM 0
Share

That won't return the pixel coordinates? Checked and it still reports pixel coordinates. I need the world position, not screen position.

avatar image Dave-Carlile · Oct 31, 2013 at 12:15 PM 1
Share

Why not? According to the documentation, Input.mousePosition returns the screen (pixel) coordinates of the mouse. You asked for that in a Vector2. If that's not what you're meaning, can you provide more info?

avatar image aidenkael · Oct 31, 2013 at 12:16 PM 0
Share

To be more clear, I need the world position, not the screen position

avatar image Dave-Carlile · Oct 31, 2013 at 12:20 PM 0
Share

Ah, okay. You're looking for Camera.ScreenToWorldPoint then.

You can pass the mouse position into it.

Here's a forum discussion on some potential gotchas when using this: http://forum.unity3d.com/threads/27560-ScreenToWorldPoint-Problem

avatar image Dave-Carlile · Oct 31, 2013 at 01:26 PM 1
Share

Refer to this: http://answers.unity3d.com/questions/331558/screentoworldpoint-not-working.html

You have to pass a depth, which gives you a point on a line that goes from the camera into the scene. A depth of zero gives you the camera position.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Checking if the Mouse Has a Middle Button 1 Answer

C# move main camra via mouse position on boarders around screen script for RTS Game 0 Answers

How to rotate an object around another facing to mouse? 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