- Home /
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;
}
}
Could someone please help me with this? I've been stuck forever with this now.
I might have done some things differently, but I don't see anything wrong in your code. The above code should rotate the specified object the degrees specified in the time specified. Is this not what is happening, or is your issue with combining the three rotations, or...?
Yeah, the code worked, it was just extremely heavy, dropped my FPS to about 10. Figured that might be since I accidently called it every frame though... $$anonymous$$issed that for some reason. :D
But I would be interested in how you would do it? I don't feel like a StartCoroutine is the best either, just could not come up with another idea.
Answer by TobiasJohansson · Apr 22, 2014 at 10:48 AM
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.
Take a look at the docs for Quaternion.Euler:
Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order).
So if you want to change the X rotation of the element, you need to set your axis as either Y or Z.
Answer by FlaflaTwo · Apr 21, 2014 at 04:56 PM
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.