Math Assistance; Difficulty with Rotating Light

I have a dilemma and need more math assistance than scripting; I can’t help but feel the answer is obvious, and I’m drawing a blank.

I have a game object that keeps track of the in-game time and displays it. The time can be sped up or slowed down, and right now, it’s set to a 12 hour cycle with a flag for AM/PM.

Meanwhile, I also have a game object similar to a sun which rotates on the x-axis in relation to the passing time.

I’m having difficulties ensuring that the sun game object is pointing in the same direction every time it is a given time. For instance, at 12pm I want the rotation to be 90 degrees - everytime it is 12pm, regardless of how the time was sped up or slowed down in the interim.

Any help to lead me towards the right formula to accomplish this would be greatly appreciated.

For starters, why not use a 24 hour cycle? It makes the math simpler.

But, for your setup, I’d do something like the following:

// Assume bool isAM = your AM/PM flag
// Assume float cycleTime = your cycle time

float currentTime = isAM ? cycleTime : cycleTime + 12f;
float normalized = currentTime / 12f;

// Offset for 0 degrees at 6 AM
normalized -= 0.5f;

float sunDirection = normalized * Mathf.PI;

That will set the sun direction to 90 degrees at 12 PM, each respective horizon at 6 AM/PM and 270 degrees at 12 AM.

Thank you for your help. I’ll try to implement your suggestion asap :slight_smile: