- Home /
How do i make a delay in milliseconds?
Hello.
I've been using unity for quite a while now and as far as i can remember i allways had a problem with using the yiel Waitforseconds(); command. I've figured it out recently and now it works fine, but i realised thet now i need a more precise time. Is there a way to make a delay that waits for inputed miliseconds? thanks
WaitForSeconds takes a float in seconds. In order to wait milliseconds, you could divide by 1000 or just use a decimal number.
$$anonymous$$G. (javascript): yield WaitForSeconds(0.001); //This is 1 millisecond
or C# [$$anonymous$$ust be IEnumerator of sorts): yield return new WaitForSeconds(0.001); //This is 1 millisecond
Answer by Owen-Reynolds · Nov 23, 2014 at 03:24 PM
You really can't make a specific small delay. Coroutines run at most every frame. WaitForSeconds(X) really says to keep skipping frames until at least X time has passed. In other words, if you're at 50FPS, Wait rounds up to the nearest 0.02.
As a quick test, can try a Wait loop using 0.01, 0.001, 0.0001 and see if it makes any difference.
Answer by Landern · Nov 15, 2014 at 10:17 PM
WaitForSeconds take a float for a argument/parameter. So if you wanted to wait one second:
// C#
yield return new WaitForSeconds(1);
// js
yield WaitForSeconds (1);
but if you wanted to wait for 1 millisecond:
// C#
yield return new WaitForSeconds(0.001F);
// js
yield WaitForSeconds (0.001);
There's no difference between 0.001 and 0.01. Coroutines are not that accurate down to the tenths or hundredths of a second it seems. I built a stopwatch using a coroutine that used WaitForSeconds(0.001)
to count by milliseconds and I timed it against a real stopwatch. The Unity stopwatch using a coroutine was 24 seconds slower after one $$anonymous$$ute on the real stopwatch.
It's worse than that - they aren't even trying to be accurate. See the 1st line of my answer below. If you like, look at the page with the entire Unity ti$$anonymous$$g sequence on it.
There is a difference, but it depends on your framerate. If you don't have at least 1000 frames per second you can't measure 1 ms. Coroutines (and pretty much everything else) run on the main thread of Unity and that's in sync with your visual frame rate. If a coroutine yields with WaitForSeconds it can only continue running the next frame. So whatever your frame rate is limits your "time resolution".
If you need anything more precise you have to use a seperate thread. $$anonymous$$eep in $$anonymous$$d that the Untiy API is not thread safe. So to interact with the thread you need to use proper synchonisation / locking. Also Unity prevents you from calling any Unity API method from a different thread than the main thread.
However if you just want to create a stopwatch, why don't you use a high precision counter / Stopwatch? They use either the system time, the system clock or a the high precission timer hardware on your PC to measure time- In general when you want to measure time you usually just store the start time and at the end (as well as at intermediate points) you simply subtract the start time from the current time to get the elapsed time. Depending for which purpose you need this, simply using Time.time
might be enough. But again Time.time only updates one each frame so the time resolution is still bound to your framerate (though the error does not accumulate). So if you measure a time of 12 seconds and 342 ms the result might be 12 seconds and 326 ms or 358ms at about 60 fps per second (60fps --> 1/60 == 0.016666 --> ~ 16.6 ms). At 60fps the error is about 16 ms. At 120fps it is about 8 ms.
You can also use Time.realtimeSinceStartup
Well using another thread for ti$$anonymous$$g the game event won't help you anyway, because you will have to sync with the unity thread anyway, and that you will do only when the next frame happens
Answer by filipeoprado · Mar 08, 2019 at 04:57 PM
Try this:
float delayInMs = 0.01f;
float ms = Time.deltaTime;
while(ms <= delayInMs )
{
ms += Time.deltaTime;
yield return null;
}
//Do your stuff
Your answer
Follow this Question
Related Questions
C# simple delay execution without coroutine? 2 Answers
Can IEnumerator be called more than once? 1 Answer
yield WaitForSeconds to delay the instantiation of a game Object; 1 Answer
30 Objects all firing at exactly the same time and not randomly 1 Answer
How to delay a function (how to use WaitForSeconds() and yield) 1 Answer