Rotate sun during set of time

I have searched and found no answers that work for what I’m trying to do. So now I really need help.

I want to make a day and night cycle in my game. For this I want the sun to move certain degrees during different timesets of the day. For example during sunrise the sun should move 60 degrees, and during day it should move 120 degrees. And I want the sun to do this during the time spent in each section so when I reach the next time set (for example from sunrise to day)it should have reached the final degrees.

So if my sunrise is 10 seconds I want the sun to move 60 degrees in 10 seconds. And when I reach 10 seconds my timeset will turn into day and then I want the sun to move 120 degrees in 20 seconds. So the sun always moves, and it moves the amount of degrees it should during each timeset. However I have not been able to do this.

This is the last function of like a million I tried without luck.

``````    IEnumerator RotateSun(Vector3 fDegrees, float SecondsDuringTimeSet)
{
//lSunLight.transform.eulerAngles = new Vector3(fDegrees, 0, 0);
Quaternion quaFromPosition = lSunLight.transform.rotation;
Quaternion quaToPostion = Quaternion.Euler(lSunLight.transform.eulerAngles + fDegrees);

for (float t = 0.0f; t < 1; t += Time.deltaTime / SecondsDuringTimeSet)
{
lSunLight.transform.rotation = Quaternion.Lerp(quaFromPosition, quaToPostion, t);
yield return null;
}
}
``````

Mm, I kind of got the code to work now. But still have one problem. It does not only rotate arond the x-axis for some reason. After a while I see that both y and z also changes and this screws up my day and night since it then after a while makes the y and z higher and the sun starts rotating more and more on the side, which change the time it takes to get to a certain degree.

``````// Call and use the Coroutine
fMoveSun = 20.0f;
StartCoroutine(RotateSun(fMoveSun, fConvertSunriseToGTSeconds));

// The Coroutine
IEnumerator RotateSun(float fDegrees, float SecondsDuringTimeSet)
{
Quaternion quaFromPosition = lSunLight.transform.rotation;
Quaternion quaToPostion = Quaternion.Euler(lSunLight.transform.eulerAngles + new Vector3(fDegrees, 0, 0));

for (float t = 0.0f; t < 1; t += Time.deltaTime / SecondsDuringTimeSet)
{
lSunLight.transform.rotation = Quaternion.Lerp(quaFromPosition, quaToPostion, t);
yield return null;
}
}
``````

Does anyone know how I can stop this? I thought I already did that by setting the y and z to 0 for each rotation but apperently that does not work.

This code will work if the sun object you are referring to is simply a directional light. If you have something like a flare attached to it (in other words, you want the sun to physically move across the sky) you need to use Transform.RotateAround(). Transform.Rotate only affects the local rotation of the object - so it simply rotates around its center of mass. Thus, in this code your sun is rotating (so you would see it if the sun has a directional light), but the position of the sun remains the same.