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°?
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:
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.