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 nintorii · Aug 15, 2014 at 10:21 PM · coroutinetimemathfor-loop

Run coroutine X amount of times in Y seconds?

I am trying to keep track of military time in my game by using a coroutine that would supposedly loop through 2400 times in the number of seconds it takes for a complete day cycle in my game. I'm using 30 seconds for testing purposes.

Here's my code.

 public float milTime;
 public int dayLength = 30;
 
 void Start () {
         StartCoroutine("ChangeTime");
     }
 
 public IEnumerator ChangeTime() {
         for (int t = 1; t < 2400; t += 1)
         {
             milTime = t;
             yield return new WaitForSeconds(dayLength/2400f);
         }
     }

In theory, this could should work, right? I increment military time by 1, starting at 1, waiting 30/2400 seconds between each increment. The problem is, it doesn't. The military time counter is considerably far behind by the time 30 seconds go by. By a couple hundred. I would guess that this would work if one day was 2400 seconds (2400/2400 = 1, increment by 1 every 1 second), but I'm not about to wait 2400 seconds to find out. Is this a problem with the way Unity handles such small numbers as 30/2400 as a wait time, or do I have a logic error somewhere? Thank you!

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

3 Replies

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

Answer by nintorii · Aug 16, 2014 at 06:06 AM

Sorry if the question was hard to understand or I didn't fully comprehend some of the suggestions I got, but here's the solution I found to my problem. This will get my value from 0 to 2400 in the time specified without much margin of error.

 public float milTime;
 public int dayLength = 30;
 
 void Start () 
 {
     StartCoroutine(SetTime (0, 2400, dayLength));
 }
 
 private IEnumerator SetTime(float startValue, float endValue, float moveTime)
 {
     float curTime = 0f;
     while (curTime < moveTime)
     {
         milTime = Mathf.Lerp(startValue, endValue, curTime / moveTime);
         curTime += Time.smoothDeltaTime;
         yield return null;
     }
 }


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
0

Answer by Eric5h5 · Aug 15, 2014 at 11:01 PM

Waiting for .0125 seconds won't really work, since the Unity update rate is tied to the framerate. It will wait for the nearest amount to .0125 that's possible, but it will almost always be off by a relatively significant amount each yield, and that will become cumulatively worse over time. (Unless the framerate is insanely high.)

It would be better to have a timer that you set to 0 in Start, then add Time.deltaTime * someFactor every frame.

Comment
Add comment · Show 4 · 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 nintorii · Aug 15, 2014 at 11:56 PM 0
Share

That answers my question, thank you! Your suggestion works, but only if I find a constant by hand each time I want to change the time of a day cycle, and even then, it's slightly off. Is there any way to use the timer method using the length of the day as a variable?

avatar image aman_jha · Aug 16, 2014 at 12:02 AM 0
Share

$$anonymous$$ark it checked on the left of the answer

avatar image nintorii · Aug 16, 2014 at 12:08 AM 0
Share

@Yoman It answered my question about whether or not my solution was correct and it was just a Unity/frame-rate issue or my code's logic, but it doesn't really solve the problem at hand. Still useful though. edit: still unable to find a solution :/

avatar image Eric5h5 · Aug 16, 2014 at 04:31 AM 1
Share

I'm not really sure I understand the issue...you're just counting from 1 to 2400 at an arbitrary rate, yes? So you'd multiply Time.deltaTime by that rate, no need to find anything by hand. Although I should mention that this isn't technically military time since hours have 60 $$anonymous$$utes in them, not 100; not sure if that matters for what you're doing.

avatar image
0

Answer by Kiwasi · Aug 16, 2014 at 04:12 AM

Use a custom property to convert the game time into your time.

 public float milTime {
     get {
         return (Time.time/30);
     }
 }
Comment
Add comment · Show 1 · 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 Kiwasi · Aug 16, 2014 at 04:35 AM 0
Share

Its also possible to correct for offset errors in your coroutine by comparing the cycle number to Time.time

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Lerp to position, after a while come back, problem occured using Coroutines 1 Answer

How to wait for Start() to run when calling InstantiateObject() 1 Answer

RPG Action Progress Bar always same speed 1 Answer

A loop FOR waits for a coroutine to end 1 Answer

Time Manager Class Implementation Like Coroutine in Unity 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