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
1
Question by SteamGear · Apr 26, 2014 at 09:06 PM · mouse positionscreen-coordinates

Resolution independent mouseposition

Hello simple question, how do i get the mouseposition independet? I use scripts like:


 mouseposition=Input.mousePosition-new Vector3(Screen.width/2,Screen.height/2,0f);
 
 transform.Rotate(0f,mouseposition.x*Time.deltaTime,mouseposition.y*Time.deltaTime,relativeTo:Space.Self);


For a player using 1680*1050 the highest x is 840, but for a player using 800*600 the highest x is 400=> the player is turning slower, how do i fix this?

Comment
Add comment · Show 1
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 SteamGear · Apr 26, 2014 at 10:11 PM 0
Share

thank you, it works fine now :)

2 Replies

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

Answer by ByteSheep · Apr 26, 2014 at 09:51 PM

You will have to convert your code slightly to use normalized screen positions like this:

 // return the normalized mouse position (between 0 to 1) relative to the screen
 //will be 0 if the mouse is on the very left
 //will be 1 if the mouse is on the very right
 
 float mouseRatioX = Input.mousePosition.x / Screen.width;
 float mouseRatioY = Input.mousePosition.y / Screen.height;
 
 //now create a new vector3
 
 mousePos = new Vector3(mouseRatioX - 0.5f, mouseRatioY - 0.5f, 0f);
 
 //mousPos.x and mousPos.y are now between -0.5f to 0.5f
 //you could normalize these values if you wanted to get values between -1f to 1f instead
 
 //create a simple rotate speed variable
 float rotateSpeed = 100f;
 
 //now rotate, the rotation speed should now be resolution independent
 transform.Rotate(0f, mousePos.x * Time.deltaTime * rotateSpeed, mousePos.y * Time.deltaTime * rotateSpeed, Space.Self);
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
7

Answer by robertbu · Apr 26, 2014 at 09:44 PM

One solution is to use Viewport Coordinates. Viewport coordinates go from 0.0 in the lower left corner of the screen to 1.0 in the upper right of the screen.

 Vector3 mousePos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
 mousePos -= new Vector3(0.5f, 0.5f, 0.0f) * factor;

You can then use mousePos.x and mousePos.y in your Rotate(); You will need to define and initialzie 'factor'. Given your current code, start with a value of 500 for 'factor'.

Comment
Add comment · Show 2 · 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 ByteSheep · Apr 26, 2014 at 09:54 PM 0
Share

Darn it, this always happens :) Guess I should always refresh the page to check if any answers have been posted in the meantime, before posting my answer.

avatar image robertbu · Apr 26, 2014 at 10:08 PM 0
Share

It happens to me too. Your answer is better explained, and underneath it is the same answer.

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

22 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

Related Questions

increase linecast length 0 Answers

Get Project View position in editor 1 Answer

Problems with turret rotation clamp 1 Answer

How can I shoot a projectile into mouse position? 2 Answers

GameObject rapidly changing position (flickering) 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