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 Juanpe09 · Feb 13, 2016 at 03:24 AM · unity 5rotationrotateanglesoperator

Why the subtracting compound assignment operator is used to ADD a angle in unity?

Hello, I'm reading the book "Unity in Action: Multiplatform Game Development in C#", and I can't understand how the "-=" operator works with angles to perform a rotation , and the author doesn't explain this either. I know what the "-=" operator does (subtract an assign a value to the left variable), but why is used here:

 ...
     public float sensitivityHor = 9.0f;
     public float sensitivityVert = 9.0f;
     public float minimumVert = -45.0f;
     public float maximumVert = 45.0f;
     private float _rotationX = 0;
     void Update() {
     if (axes == RotationAxes.MouseX) {
     transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
     }
     else if (axes == RotationAxes.MouseY) {
     _rotationX -= Input.GetAxis("Mouse Y")*sensitivityVert;//Why I can't use the "+= operator"?
     _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
     float rotationY = transform.localEulerAngles.y;
     transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0); //Values of _rotationX
 //are all negatives?
     }
     ...

I think that has to do with how unity handles the angles of rotation. What not use the "+=" compound assignment operator that is in fact the operator to ADD a value, an instead uses the "-=" operator that is for subtract a value, what is subtracting? Then all the values of "_rotationX" are negatives? inclusive the posives that the input function "GetAxis" retrieves?

   _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert; 
         _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
         float rotationY = transform.localEulerAngles.y;
         transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);

I have checked an the script only works with the "-=" operator, something is going on behind the scenes that the book doesn't explain, can anyone tell me how this work in unity?

Her is what the book says about this:

"The Rotate() method increments the current rotation, whereas this code sets the rotation angle directly. In other words, it’s the difference between saying “add 5 to the angle” and “set the angle to 30.” We do still need to increment the rotation angle, but that’s why the code has the -= operator: to subtract a value from the rotation angle, rather than set the angle to that value. By not using Rotate() we can manipulate the rotation angle in various ways aside from only incrementing it."

Any help would be greatly appreciated!

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

Answer by NoseKills · Feb 13, 2016 at 09:40 AM

They are using -= to get the rotation to happen to the desired direction compared to the mouse movement. It's just a question of "do we want negative to mean up or down". You could get the same behavior by putting the negative sign in other places and using +=

 _rotationX += Input.GetAxis("Mouse Y")*-sensitivityVert;
 
 // OR
 
 _rotationX += -Input.GetAxis("Mouse Y")*sensitivityVert;
 
 // OR
 
 public float sensitivityVert = -9.0f;
 // AND
 _rotationX += Input.GetAxis("Mouse Y")*sensitivityVert;

Using -= doesn't mean you can only get negative values. In general, if you subtract a negative value from value X, the result is greater than X. In this case they want rotation_x to grow when "Mouse Y" input is less than 0 because that causes the object to rotate in the direction they wanted.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Drag - Rotate 0 Answers

it is possible to rotate a texture ? 0 Answers

Applying a rotation relative to the object's starting rotation 0 Answers

Rotate object with 90 degrees? 1 Answer

Problems With .rotate behavior 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