Hey, so I want to rotate the standard DirectionalLight in Unity3D according to the current actual hour or second of the day (or rather, of the system that the game is played on.) The idea is, of course, to have the same time and sun-position in the game as it is in real life. I know I can use
to get the curent time in an HH.mm.ss AM/PM format, but I need the exact hour, or even better, the exact second. I can’t seem to find anyone ever needing this before me, so I couldn’t find a solution yet.
This is already provided with the DateTime object.
float offsetAngle = 0f; // Think of this like a time zone
float hourAngle = 360f / 24f;
float minuteAngle = 360f / (24f * 60f);
float secondAngle = 360f / (24f * 60f * 60f);
DateTime currentTime = System.DateTime.now;
float currentSunAngle = offsetAngle +
currentTime.Hour * hoursAngle +
currentTime.Minute * minuteAngle +
currentTime.Second * secondAngle;
With this currentSunAngle will rotate from 0 - 360 in 24 hours. You can use that for your rotation. You’ll need to play around with the offsetAngle to determine what “time zone” you’re in. 13:00:00 GMT will be morning for some people, and evening for others.
I have absolutely no idea how I didn’t find the appendices .Hour, .Minute and .Second when I kept typing after System.DateTime.now.
Sorry for wasting your time and thanks for the advice, Kishotta even provided a solution to what I had in mind. That’s amazing, thank you so much!