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
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.
Your answer
Follow this Question
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