# Quaternion Rotation Issue

I’m making a day/night cycle for a game, so I need the main light to rotate. The code for this is as follows:

void Update(){
rotation = (min / 24) * 360;
//This finds the rotation based on ‘percentage’ of time out of 24 minutes.

transform.rotation = new Quaternion (rotation - 90.0f, 0.0f, 0.0f, 0.0f);
//This sets the rotation, -90 makes 6A.M. sunrise, which is what I wanted.
}

My problem is that the rotations always come out weird. Say it’s 7A.M. (7 / 24 * 360) - 90 = 15, so the rotation along the x-axis should be 15 degrees. But come 7A.M, the rotation becomes: (0, 180, 180).

Why would this happen? Nowhere in my code do I alter the y or z axis, but they end up changing. I tried to display the value of ‘rotation’ with debug.log(), and it did come out to 15. Does it have something to do with the ‘W’ axis?

I hear this repeated alot around here “Never mess with Quaternions unless you absolutely know what you are doing”. I’m hopeless with rotations in general, so I can’t comment on the ‘why’ of your situation except to say Yes, it’s cuz of the ‘W’.

But I recently worked on my Day/Night cycle and a fellow UAer posted some pretty complete code on it, hopefully you can adopt it to make yours work too