- Home /
Why does transform.Rotate behave so strange?
Hello,
I hope you can help me understand how transform.Rotate works. (I have already checked the Documentation, didn't help...)
I have a light and wrote a script for it to make it rotate, to achive a Day-Night-Cycle. Now I want it to rotate faster at Night, so night is shorter. To do that I wanted to implement a if check to rotate it faster after a certain degree.
I wanted to find out when I need to rotate it faster, so I went ingame and watched the rotation Values. Here comes the first strange thing:
Rotation Value for X is increasing until 90°, stops at 90° for a second and then is getting smaller again, but now Y and Z are rotated 180° ; it rotates unitl 270° and then gets bigger again and Y and Z are 0° again.
So I setup my script to rotate faster when Sunlight.transform.rotation.x > 270
That didn't work. So I logged the rotation of the Sunlight. The values basically make a Sine from -1 to 1.
Why is it behaving like that? Why aren't the rotation values just increasing until 360° and then jump back to 0°?
Answer by Baste · Oct 23, 2014 at 03:35 PM
The issue that you're running into is due to the fact that Unity uses something called Quaternions internally to represent rotations, instead of a 3-dimensional vector. A quaternion has 4 coordinates, uses complex numbers, and is pretty difficult to understand.
To do what you want to do, check the Euler Angles of the transform's rotation. so instead of:
Sunlight.transform.rotation.x > 270
do this:
Sunlight.transform.rotation.eulerAngles.x > 270
Hope that helps.
Ok thanks, I will check that out! :)
Edit: Cool! Worked just like I wanted! Thanks!
Can you mark the answer as accepted? That'll remove the question from the queue of unanswered questions.
Answer by robertbu · Oct 23, 2014 at 03:32 PM
Read my answer here:
http://answers.unity3d.com/questions/799824/oafatwhy-does-transformrotationx-45-not-work.html
In the absence of your code, it is difficult to give specifics. If you are only rotating around one axis, then my suggestion is to keep track of the total rotation. That is treat the eulerAngles as write-only and track your own value:
if (angles.x > 180 && angles.x < 360) {
s = speedFast;
}
else {
s = speedSlow;
}
angles.x += s * Time.deltaTime;
angles.x = angles.x % 360.0;
transform.eulerAngles = angles;
'angles' is a Vector3 initialized to the starting angles of your object.
Note that unlike Rotate(), assigning to eulerAngles is a world rotation, but since you are only rotating on one axis, this will not make a difference.
Uhh, having a seperate value and doing incremental changes to both is not a good ides. Since the rotation of the Gameobject is stored as Quaternion the two "angles" might drift slowly. You should set a fix rotation based on your angle. This has several advantages, especially when you want to set the "current time".
Hmmm... I still don't fully understand why it does what it does...
But your idea of solving the problem is great! Thanks!
Edit: Bunny83 how would I do that? I don't understand what you mean right now...
Yes, I see. Strangly with this code, the object just stops rotation at 90°
That is called a Gimbal Lock. This happens when a gimbal system (like the eulerangles) rotates the 2nd gimbal by 90°. You will effectively loose one degree of freedom because both the y and z axis are now identical.
Quaternions don't have this problem and that's why they are actually used. However Quaternions can't be viewed / edited in a human-readable form, that's why Unity converts the quaternion into euler angles, where we have again our gimbal-lock problem.
That's why when unity converts a rotation that points upwards into eulerangles you can get different results since eulerangles can represent certain rotations in multiple ways. The euler angles representation is not unique. That's why reading the euler angles is not recommended for such things.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Problem in rotation of plane 1 Answer
Change Pivot Point- Maya 3 Answers
Twin Stick Shooter controller 0 Answers
Controlling a rotating object 2 Answers