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 Tafk · May 23, 2013 at 09:40 AM · time.deltatimetimescalecounterbasics

Using Time.deltaTime as time counter.

Recently I faced a little problem. In my game (and I'm pretty sure in yours too) I use this construction very often:

 void Update()
 {
    sinceLastAction += Time.deltaTime;
    if (sinceLastAction > actionTimeout)
    {
       DoAction();
       sinceLastAction = 0;
    }
 }

But when actionTimeout has low float value (like 0.5 or 0.1) you can stuck in situation when Time.deltaTime has half or even greater value of your time counter (especially when you manipulate with Time.timeScale), and it's bring problem when part of your time just get deleted each Update(). Other example, your game got lag or something and Time.deltaTime pretty high, in some cases you can have more then one not executed actions. So is my question, should I use constructions like this:

 void Update()
 {
    sinceLastAction += Time.deltaTime;
    if (sinceLastAction > actionTimeout)
    {
       int timesActionHappend = Mathf.FloorToInt(sinceLastAction/actionTimeout);
       for (int i = 0; i < timesActionHapped; i++)
          DoAction();
       sinceLastAction -= actionTimeout * timesActionHappend;
    }
 }

, or I just misunderstanding some basic principles of time based programming?

Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · May 23, 2013 at 09:45 AM

This can be simplified to:

 void Update()
 {
     sinceLastAction += Time.deltaTime;
     while (sinceLastAction > actionTimeout)
     {
         DoAction();
         sinceLastAction -= actionTimeout;
     }
 }
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 Tafk · May 23, 2013 at 09:59 AM 0
Share

ye, you right! Thanks! Is there any possibilities to not write this constructions each time I want to create simple timer? Like this:

 public float attackTimeout;
 
 void Awake()
 {
    Timer.Add(attackTimeout, Attack);
 }
 
 public void Attack()
 {
    //stuff
 }
avatar image Bunny83 · May 23, 2013 at 10:38 AM 1
Share

Sure! I'm wondering where Fattie is ;) He always tries to force everyone to use InvokeRepeating ;)

avatar image Tafk · May 23, 2013 at 11:21 AM 0
Share

InvokeRepeating is nice, but problems starts when you'll try to save state of invocation, ex.: when you want save to file, you can just write sinceLastAction variable, and then read it, but when you use Invoke family methods, I have no idea how to store state of invocation. If you have any thoughts about it, please tell me ;)

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

14 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

Related Questions

Displaying Time left 1 Answer

Unity Time.timeScale is out of range. Any chance to set it to 360? 1 Answer

2nd independent deltaTime and timeScale variables, or a way to mimic this? 1 Answer

Time.deltaTime returns smaller values than actual time between frames 0 Answers

How do I make the timeScale not affect the timer? 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