Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Breaking_Reality · Aug 14, 2018 at 06:43 PM · cameramouse

Can someone tell me how this code works?

Hello! I found this code on the forums but I don't understand it fully on why it works. I want to understand why it works so next time I do this I know how to do it and why it works this way. Here is the code:

     float mouseX = (Input.mousePosition.x / Screen.width) - 0.5f;
     float mouseY = (Input.mousePosition.y / Screen.height) - 0.5f;
     transform.localRotation = Quaternion.Euler(new Vector4(-1f * (mouseY * 180f), mouseX * 360f, transform.localRotation.z));

This goes in the update and what it does is make the camera move to your mouse.. Like an fps game. I want to know why we divide the mouse input by the Screen width/height and why subtract it by 0.5.. And why we do that math in the Quaternion.Euler to make it work properly. Also I have no idea how to add sensitivity to this. Anyone feel free to answer I simple want to learn! 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
0
Best Answer

Answer by Hellium · Aug 14, 2018 at 07:19 PM

Input.mousePosition.x is defined in the Screen space, i.e, it can take values between 0 and Screen.width. Dividing Input.mousePosition.x by Screen.width returns a value clamped between 0 and 1. Substracting by 0.5 "shifts" the range between -0.5 and 0.5


The same operations are performed on Input.mousePosition.y.


Then, the rotation of the object is performed using euler angles:


The angle around the pitch axis ( x axis in Unity) is computed according to the mouse position on the vertical axis: -1f * (mouseY * 180f) means that the object will rotate "up" or "down" between 90° (if mouseY = -0.5, meaning the mouse in at the bottom of the screen), and -90° (if mouseY = 0.5, meaning the mouse in at the top of the screen)


The angle around the yaw axis ( y axis in Unity) is computed the same way : the object will rotate "left" or "right" between -180° (if mouseX = -0.5, meaning the mouse in at the left side of the screen), and 180° (if mouseX = 0.5, meaning the mouse in at the right side of the screen)


The final angle around the roll axis is kept as it is.


Finally, the computed euler angles are converted into a Quaternion using Quaternion.Euler and becomes the new local rotation of the object.


X axis : [0, Screen.width] → [0,1] → [-0.5, 0.5] → [-90, 90]

Y axis : [0, Screen.height] → [0,1] → [-0.5, 0.5] → [-180, 180]

Comment
Add comment · Show 9 · 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 Breaking_Reality · Aug 14, 2018 at 08:01 PM 0
Share

@Hellium Wow nicely explained! Thank you. One question though. What if I want to lock the mouse? This will no longer work because it is locked to the center I assume. Any ideas on that or should I just hide the mouse? Either way the user will be forced to look around with the limits of their screen. This is because when the mouse hits the edge of the screen there is no more movement of the camera even if I keep moving the mouse as if I wanted to keep turning my head. I'm not sure what to do at this point. Thanks!

avatar image Hellium Breaking_Reality · Aug 14, 2018 at 08:41 PM 0
Share

I guess you can try something like the following (code not tested)

 private Vector2 mousePosition = Vector2.one * 0.5f;
 
 private void Start()
 {
         Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
         Cursor.visible = false;
 }
 
 private void Update()
 {
     mousePosition.x = $$anonymous$$athf.Clamp( mousePosition.x + Input.GetAxis("$$anonymous$$ouse X"), 0, Screen.width ) ;
     mousePosition.y = $$anonymous$$athf.Clamp( mousePosition.y + Input.GetAxis("$$anonymous$$ouse Y"), 0, Screen.height ) ;
 
    // The code you currently have
    // Just replace `Input.mousePosition` by `mousePosition`
 }

avatar image Breaking_Reality Hellium · Aug 14, 2018 at 09:07 PM 0
Share

@Hellium Great fix but now I have another issue! So you know how the camera goes up and down? You can basically go around as many times as you want. I tried a mathf.clamp and it works but then we have the issue of the mouse input. If the mouse input goes over 90 or -90 the camera clamps but now if you are at -90 (Looking up) and want to look down you have to keep moving your mouse down until you hit -90 and lower depending on how far you moved your mouse over -90 even though its clamped. If that made any sense.

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.

Follow this Question

Answers Answers and Comments

214 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 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 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 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 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 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 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 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 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 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

Unity 3D: Third person movement and mouse camera control 0 Answers

Mouse Look Script Not Working 1 Answer

New Input System first person camera stutter. 0 Answers

Object Rotate with Camera 2 Answers

drag AND drop push and pull object 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