- Home /
Make a change every 0.02 seconds precise
I want to make something to change it's position every 0.020 seconds and after it finishes one cycle, speed it up to 0.019 and so on. I tried working with decimal but still not precise enough. Please help ^^
you can change its position within the system every 0.02 seconds, however how you actually view its position changing will be dependent on the frame rate of the computer, could you expand on why you are trying to do this!?
It's not possible to guarantee ti$$anonymous$$g of execution of any function at that level of precision. Perhaps if you explain why you're trying to do this we can suggest alternative approaches.
I think what works better is to think in terms of speed/second. To increase the speed, increase the speed (ins$$anonymous$$d of decreasing the time unit, keeping speed constant.)
If you have integer movement, round the "use me" value to the nearest. If you move 40 times during a 60-frame second, it will be in 20 positions for 1 frame, and 20 positions for 2 frames. But there is no way to avoid that.
@tanoshimi calling InvokeRepeating("SomeFunction", 0, 0.02f);
with:
void SomeFunction(){
Debug.Log(Time.time);
}
prints exactly 0.02, 0.04, 0.06 etc...
is this 'faked' somehow or how does it happen to be exact.
I also tried:
IEnumerator SomeFunction(){
while(true){
Debug.Log(Time.time);
yield return new WaitForSeconds(0.02f);
}
}
and sure enough it is way out with what time the function calls itself
Can you explain this? :O
Thanks,
Scribe :)
Answer by ozturkcompany · Apr 04, 2015 at 03:22 PM
I do think its not possible but i suggest you looking at how Time.deltaTime increase by time. Create a variable and set it to something like;
private var a : float = 0;
function Update()
{
a += Time.deltaTime;
Print(a);
}
See how a value increases per frame, then you might have to see the chance whether it is possible to detect that small amount of change in seconds. Cheers.
Hmmm, i think that i have found you a possible solution. Why won't you look at your system's time? You can read system's time and it's miliseconds. 1000 miliseconds is 1 sec. So it might be handy for your case.
An Update takes 0.016 aprox each frame to take and a fixedupdate taxes 0.02 seconds each frame. I was thinking that maybe the global times is measured in 0.1 miliseconds, dunno :(
I think that it is worth to shot to try System's time and use it's miliseconds
#pragma strict
private var t : float = 0;
function Update ()
{
CompareValues ();
}
function CompareValues ()
{
t += Time.deltaTime;
Debug.Log(t);
if($$anonymous$$athf.Approximately(t, 0.02))//O$$anonymous$$, we got the value!
{
Debug.Log("YES!" + ", Difference is: " + (t - Time.deltaTime));
}
if(t > 0.02)//OH, we missed it...BUT let's say we got the value since it will be pretty to close to what we want!
{
Debug.Log("YES!" + ", Difference is: " + (Time.deltaTime - t));
t = 0;
}
}
This is the closest you can get...Sadly it is not possible.
Your answer
Follow this Question
Related Questions
How to smootlhy change values in amout of time? 3 Answers
Shrink and change color of GUI 0 Answers
Unity seems to be taking a long time destroying a scene? 0 Answers
Color lerp once? 2 Answers