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 spiralgear2 · Jul 23, 2013 at 09:46 PM · yieldcoroutineswhile-looptime.time

Time.time undependable in a coroutine?

I have this script which is called from the start function as a coroutine:

 function RotateBasedOnCurve(){
 
     var animating : boolean = true;
     var animStartTime : float = Time.time;
     
 
     while(animating){
     
         var localTime : float = Time.time - animStartTime;
         
         print(localTime + " " + EquipAnimCurve.length);
     
         tarObject.localRotation.eulerAngles.x = EquipAnimCurve.Evaluate(localTime);
         
         
         if(  Mathf.Approximately(localTime, EquipAnimCurve.length/2)  || localTime == EquipAnimCurve.length/2){
             Debug.Log("We are half way through");
         
         }
     
         if(localTime > EquipAnimCurve.length){
             print("function over");
             animating = false;
         }
         
         yield;
     }
     
     
 
 
 }


Note: the || is just a redundancy I put in while troubleshooting

The rotation follows the curve correctly, and the "function over" print is called, but the debug log "We are half way through" is never called. The local time prints are erratic, often skipping decimal places completely.

I am not sure what is happening here, I am new to coroutines. Is Time.time not dependable in a coroutine? And if not, how can I keep track of time inside this while loop?

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

Answer by Bunny83 · Jul 23, 2013 at 09:58 PM

Uhm you know that your game doesn't run infinite frames per second?`That means between two frames lies some time. For example when you have a framerate of 100 frames per sec. The time between two frames is 1/100 sec. so 10ms. Since you never have an exact constant framerate it will fluctuate from frame to frame. So one frame it takes 10.0416562 seconds the other frame it takes 9.87651321 seconds. Time.time is the time the current frame has been started. It's almost impossible to "sample" an exact time.

The best solution for such "events" is to use a boolean and a statement like this:

 if( !halfWayThrough && localTime > EquipAnimCurve.length/2)
 {
     Debug.Log("We are half way through");
     halfWayThrough = true; // prevent the event from firing twice.
 }
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 spiralgear2 · Jul 23, 2013 at 10:55 PM 1
Share

Yeah I thought that is what $$anonymous$$athf.approximately was intended for. Thanks, got it working.

P.S. just in case anyone stumbles upon this code for some other reason, there is another mistake I made here. The length of an animation curve refers to the number of keys it has, not the length of the curve. To get the length you need:

 curveName.keys[curveName.length - 1].time

That will return the time specified in the final key, which is the total time of the curve.

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

16 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

Related Questions

Does a coroutine end itself automatically? 2 Answers

Trouble Resuming after Yielding while Inside Coroutine 1 Answer

Understanding yield inside a for or a while loop 2 Answers

How do I use Coroutines remotely... and correctly ? 2 Answers

Having trouble using coroutines, making unity hang 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