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
0
Question by inglipX · Aug 19, 2013 at 03:18 PM · c#timeraccuracy

Accuracy of timer issue please help

I am trying to stop my timer exactly at 5. Here is the script: float timer = 0.0f;

 void Update(){
 bool stopTimer = false;
     
     if (!stopTimer)
     timer += 1 * Time.deltaTime;
     
     if (timer >= 5.0f){
     stopTimer = true;    
     }
     
     
 }

When it stops its always a little bit over 5..... it is unaccurate and i tried if (timer == 5.0f) but then it just misses it somehow and the timer keeps going. :(

Comment
Add comment · Show 2
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 inglipX · Aug 19, 2013 at 04:47 PM 0
Share

Thank you! I'm going to try using FixedUpdate

avatar image chelnok · Aug 19, 2013 at 06:07 PM 0
Share

btw ,,shouldnt this line: bool stopTimer = false; be outside from Update?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Joyrider · Aug 19, 2013 at 03:22 PM

You'll never be on 5 seconds exactly. Your timer depends on Time.deltatime, which is the time between two frames. and that time will vary according to your scene and current actions.

Detect when it goes over the 5 seconds, and if needed for display purposes, you can assign 5 seconds exactly once you go over.

 if (timer >= 5.0f){
     stopTimer = true;
     timer = 5.0f;  
 }
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 robertbu · Aug 19, 2013 at 03:31 PM

You cannot stop it exactly at 5.0. The problem is that Time.deltaTime varies from frame to frame, and even if it did not, it is unlikely that the value would exactly divide into 5. So one frame the value is somewhat less than 5.0, and the next frame it is a bit over.

To better see what is going one, do add this line of code at line 6:

 Debug.Log(timer+", "+Time.deltaTime);

If stopping at exactly 5 seconds is important there are a couple of other approaches, but it depends on why it is important. Using FixedUpdate() for the calculation is one approach, but I believe you can lose fixed update frames under load.

Comment
Add comment · Show 3 · 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 inglipX · Aug 19, 2013 at 04:47 PM 0
Share

if I use fixedupdate will it be accurate?

avatar image Joyrider · Aug 19, 2013 at 05:03 PM 0
Share

all are as acurate, but if you want to know if you'll get a 5 seconds sharp, I doubt it, but you can try. If a multiple of the fixedTimeStep gives you 5. But still, like robertbu said, no garanties.

avatar image robertbu · Aug 19, 2013 at 05:11 PM 0
Share

First for FixedUpdate() to work, the fixedTime step must divide evenly into 5.0. By default it is set to 0.02, so the default value works. I would still do '>=' in the comparison. As for accuracy, if the app isn't under a heavy load, you should be very close...closer than Time.deltaTime. Unity will act as if everything is accurate. Unity takes actions in other places in an attempt to keep the fixedTime steps even, but there are situations they just cannot be fixed. For example a sudden heavy calculation in Update() would cause a fixedUpdate frame to be lost. I'd have to experiment to see how Unity deals with this kind of situation.

There are potential other ways to deal with this issue. If you describe the problem you are trying to solve, perhaps someone can give you a better solution.

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to integrate timer into my C# script? 2 Answers

How to create a delay between spawning of tiles. 0 Answers

Timer doesn't count 0 Answers


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