Natural rotation of sun and moon with 2 axis

Hi guys :)! I hope you can help me with a rotation problem.

Lets lay down my facts:
I’m modelling a day/night cycle and I didn’t want the sun and the moon just go directly over your head…
If we take a look at the real life, the day night cycle is basically about two rotations. (Just let us assume the sun circles the earth ;))

  1. The Y-axis. The rotation of this axis determines the time. This is my reference point to the ingame time.
  2. The Z-Axis. This basically determines the angle of the sun or the moon.

In my project I attached the sun and the moon to an empty gameobject. Lets call it “Time-Pivot”. Sun and moon are facing that Time-Pivot and are on the opposite site of each other.

I also really want to add the feature that I can choose some settings for the cycle to simulate a polarnight or polarday.
For this reason I added 3 custom floats.

1. MaxAngle
	This is the maximal angle of the sun and the minimal angle of the moon.
2. RisingAngle
	At this angle I defined that the sun is rising/setting. This doesn't has to be 0. Since on the polarday the sun actually passes that point where it would set, but it doesn't, because the risingAngle is > 0.
3.  MinAngle
	This is the lowest angle of the sun and the highest angle of the moon. In most cases it should be less then 0, then the sun is under the horizon.

I also added two other settings.

1. RisingTime
	This is the time (in ticks*), when the sun reaches the risingAngle)
2. SunsetTime
	This is the time (in ticks), when the sun reaches the rising Angle again.
So I can choose at which time I have the sunset and the sunrise.

*My ingame time is calculated through the rotation of the time-pivot for the Y-axis. So basically, if the Y-rotation of the time-pivot is 180°, it is 12:00.
I have 3600 ticks and one tick is one second.

Now comes the problem:
I managed to do the rotation around the Y-axis with Transform.Rotate(0, 0.1f, 0); So I have 1 tick per second.
This is only the Y-rotation.
I have no idea how to implement the rotation of the Z-axis (the height of the sun).
I thought about something like smoothstep, but I can’t manipulate the rotation, because it is already depending on that Transform.Rotate() thing…
It is about applying two roationspeeds at the same time, but one rotation varys quite a lot.

*I already have functions that can calculate the angle of the Z-axis depending on the time. You may refer it to something like sunAngle(ticks) ← mathematic function. But it returns the angle of the Z-Axis in Degrees. I can calculate the answer but not set it :confused:

P.S.: I’m not that experienced yet… If you have a better idea on how to implement the cycle in a good and natural way, you can present your idea instead :slight_smile:

Actually there’s an asset in the asset store for this called uSky Pro Unity Asset Store - The Best Assets for Game Making
It as a realistic mode where you can setup the longitude, latitude and height above the sea level.

After some further research and lots of practice, I could figure out how to do it:
The whole thing doesn’t rotate anymore, but rather sun and moon independent (Some more option are allowed due to this new system) . The Rotation of the sun (which determines the Time) is now World space and the rotation along the x-axis, which determines the height is now done with the following line of code:

sun.transform.localEulerAngles = new
Vector3(SunAngle(), sun.transform.localEulerAngles.y, sun.transform.localEulerAngles.z);

where SunAngle() is the function to calculate the actual height. Basically just the x-rotation is overriden. If you still have a better solution let me know :smiley: