- Home /
Unity Simple Clock Tutorial with custom time?
Hi everyone,
as a newbie I'm trying to study and modify this Unity tutorial, and more in depth the very last section, where clock is improved by an analog movement:
void Update () {
if (analog) {
TimeSpan timespan = DateTime.Now.TimeOfDay;
hours.localRotation =
Quaternion.Euler(0f,0f,(float)timespan.TotalHours * -hoursToDegrees);
minutes.localRotation =
Quaternion.Euler(0f,0f,(float)timespan.TotalMinutes * -minutesToDegrees);
seconds.localRotation =
Quaternion.Euler(0f,0f,(float)timespan.TotalSeconds * -secondsToDegrees);
}
[etc]
I would like to start the analog movement from a custom time, like for example 00:00:30 secs. If it was easy with the part that produces discrete steps, here I'm stuck despite several attempts, and still much more confused.
If I declare something like this for example:
TimeSpan timespan = new TimeSpan(0, 0, 0, 30, 0);
and during the Update() I put
timespan =+ timespan.Add(new TimeSpan(0, 0, 1));
seconds arm act speed is very fast, like if it's working like millisecond. Minute arms, in fact, works like a seconds arm, and so on. I think it's due to Update() timing, but is there a way solve this with another approach?
Any help will be very appreciated, thanks in advance.
Answer by $$anonymous$$ · Sep 26, 2014 at 05:45 PM
That may be caused because you are adding one second to TimeSpan every Game Update which of course will make clock be very fast, maybe if you try something like this?:
timespan += new TimeSpan(0, 0, 0, 0, Time.deltaTime * 1000);
This will add Elapsed time between Frames to your current Time Span
Yes, it is.
Trying your code returns an error, but anyway after some searches I've found this working:
TimeSpan mytimespan; // declared as private member
mytimespan += TimeSpan.FromSeconds(Time.deltaTime); // In update
You've been helpful pointing out Time.deltaTime: I mark as answered, thank you.
Yeah sure didnt realized that TimeSpan constructor anly accepts integers sorry but simple cast should do it ;)