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 putlucky · Oct 17, 2014 at 06:18 PM · c#rotationmouselook

Understanding The Mouselook C# Script

I want to create a character control similar to that of Dungeon Defenders: movement left right and forward/backward is the w,a,s,d keys - but rotation/turning is done with the mouse. My first instinct was to look at the mouselook script which comes with Unity for any code I could salvage. I'm pretty much brand new to programming and so understanding this script is a real challenge - it doesn't help that there are no comments within the code's body.

Things that I don't understand:

  • float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

Transform.LocalEulerAngles is described by the Unity API as: 'The rotation as Euler angles in degrees relative to the parent transform's rotation.' What is the parent transform's rotation? And why am I ADDING the mouse input along the x axis to this but focusing on the y axis with regards to the parent transform's y 'rotation'?

  • 'rotationY += Input.GetAxis("Mouse Y") * sensitivityY;'

Why do I use +=? Don't get me wrong, I know what it means, rotationY = rotationY + Input.GetAxis("Mouse Y") etc. What's the point in setting it equal to itself here if it's an empty variable? Why do I need to plus two of the same variables together here?

I don't want to bog you guys down with everything I don't understand, those two appear to be the toughest concepts for me to grasp. As a matter of preference, please do not link me video tutorials. I'd like your own personal take on the issue if at all possible.

Thank you so much to anyone who can help.

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

Answer by robertbu · Oct 17, 2014 at 06:31 PM

A bit of house keeping. Naming is always a problem when using a mouse for rotation because typically 'x' movements of the mouse results in 'y' rotation of the object, and 'y' movement of the mouse results in 'x' rotation. And depending on the code, the naming may change. So you need to take a look at where the rotation is done to sort things out:

       transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);

So you can see they are using 'Y' for the x component, and 'X' for the y component when building the Vector3. Now to your two lines:

  float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

So this line is taking the 'y' rotation of the object, and adding the Input from the x movement of the mouse to produce a new rotation. And this line.

  rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

Is adding or subtracting from 'rotationY' based on the 'y' movement of the mouse. Note that 'rotationY' is a class variable. While it starts out at 0.0, it keeps it value frame to frame. So it is not an empty variable...it only starts the game at 0.0.

'localEulerAngles' is the rotation of an object with respect to the parent. Typically this code would be placed on camera without a parent, so it would be the same as world rotation. But imagine this code was used to rotate a turret/gun on a tank. You would want the turret/gun rotation to be relative to the tank body.


One way to understand this code is to do a Debug.Log() of RotationY and RotationX, and place the code on another object (not the camera). Also it is a bit dangerous to read from eulerAngles as this code does here. It will work given strict limits to the ways something can rotate, but it will fail for arbitrary rotations. eulerAngles is derived from the the transform.rotation Quaternion, and will change representation. For example (180,0,0) is the same as (0,180,180). So you cannot use code similar to MouseLook for arbitrary rotations.

Comment
Add comment · Show 1 · 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 putlucky · Oct 17, 2014 at 06:49 PM 0
Share

Thank you :)

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

2 People are following this question.

avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Using quaternion for mouse movement? 3 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Limiting the camera's rotation using quaternions, camera sticking to top/bottom limit angles 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