- Home /
Realtime Clock? (needles)
Hello guys, i've got a clock with a needle for seconds and a needle for minutes, is there a predefined javascript function so i can have the right rotations?
i'm talking about a racing clock so it's not like tic-tac it needs to be a fluent rotation of the needle.
Any help is appreciated
thanks in advance
bio
Answer by tnetennba · Jul 22, 2011 at 03:03 PM
There is no predefined function for this. This is very simple mathematics and you will just need to apply linear interpolation to the needles rotation based on fixed time step. For example there are 60 seconds in a minute so:
360/60 = 6 degrees of rotation persecond.
And minutes:
360/ (60*60) = 0.1 degrees of rotation persecond.
Using these measurements you can then use linear interpolation between the two positions. http://unity3d.com/support/documentation/ScriptReference/Mathf.Lerp.html
Alternatively you can use the fixed update function. This is called a fixed number of times per second and can be adjusted in the edit->project setting -> time menu. That way you do not need to use linear interpolation as you know how far the needle will have to move every fixedupdate call.
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.FixedUpdate.html
(see below)
Finally please stop bumping your questions every 20 minutes there is no need to do that as everyone can see the unanswered questions.
FixedUpdate is only for physics, and shouldn't be used for this sort of thing. For example, if you decide to change the physics update rate, then your unrelated code in FixedUpdate also changes how often it runs. Furthermore, in the event of low framerates, the maximum allowed timestep prevents FixedUpdate from running as often as it usually does, which means your clock will be wrong. So, never use FixedUpdate for anything except physics behavior.
Your right i forgot about the maximum allowed timestep. Disregard the last part of that answer.
Code sample on mathf.Lerp? i don't seen to get this working :/
when trying to $$anonymous$$athf.Lerp the rotation value between 0 and 360 using Time.deltaTime the needle gets stuck rotating back and forth
How an i supposed to tell him to rotate 6 degrees every second?
Your answer
Follow this Question
Related Questions
Finish One Combo 1 Answer
Keeping track of time? 4 Answers
Breathing sound effect using timers 1 Answer
How to save and display players times using Playerprefs in Javascript? 1 Answer
A node in a childnode? 1 Answer