Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by TERMINAL333 · Nov 15, 2014 at 10:15 PM · waitforsecondsdelay

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image static_cast · Nov 15, 2014 at 10:32 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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);


Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ghostravenstorm · May 22, 2017 at 06:41 AM 1
Share

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.

avatar image Owen-Reynolds ghostravenstorm · May 22, 2017 at 07:23 AM 0
Share

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.

avatar image Bunny83 ghostravenstorm · May 22, 2017 at 07:34 AM 2
Share

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

avatar image Pangamini Bunny83 · May 22, 2017 at 10:59 AM 1
Share

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

Show more comments
Show more comments
avatar image
-3

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges