- Home /
Getting current system hour or seconds (not entire time)?
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
System.DateTime.now
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.
Thanks!
Answer by tanoshimi · Jul 02, 2017 at 04:41 PM
System.DateTime.now.Second
perhaps? https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx
I have absolutely no idea how I didn't find the appendices .Hour, .$$anonymous$$inute and .Second when I kept typing after System.DateTime.now.
Sorry for wasting your time and thanks for the advice, $$anonymous$$ishotta even provided a solution to what I had in $$anonymous$$d. That's amazing, thank you so much!
Answer by Kishotta · Jul 02, 2017 at 04:49 PM
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.
Your answer
Follow this Question
Related Questions
Breathing sound effect using timers 1 Answer
deltatime glitches 1 Answer
Fade in AudioLister over time #c 1 Answer
slow a lerp ? array/static problem 1 Answer
Move a keyframe in time only 1 Answer