• 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
Question by MrjmCyberman · Mar 14, 2018 at 10:57 AM · coroutinecoroutinesstartcoroutineenumerate

Coroutine runs Very Slow for First time Call

its very odd.i have a very simple function like this to move a game object from Point A to B:

  IEnumerator Move(GameObject go, Vector3 startPos, Vector3 endPos, float speed)
         {
             float step = (speed / (startPos - endPos).magnitude) * Time.fixedDeltaTime;
             UnityEngine.Debug.Log("step : " + step);
             float t = 0;
 
             while (t <= 1.0f)
             {
                 t += step; // Goes from 0 to 1, incrementing by step each time
                 go.transform.position = Vector3.Lerp(startPos, endPos, t); 
                 yield return new WaitForFixedUpdate(); 
             }
         }

and i call it Like This :

 StartCoroutine(Move(PlayerGameObject, PlayerGameObject.transform.position , tmpPos, 25);

the first time call is very Slow no matter what the speed is , even2000.

but when i call it again with exactly same parameters it runs perfectly good as expected.

any idea about this? am i missing somthing?

Comment

People who like this

0 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 MrjmCyberman · Mar 14, 2018 at 11:01 AM 0
Share

according to https://forum.unity.com/threads/startcoroutine-expensive-on-first-invoke.392652/ it seems the first call is resource expensive naturely. any idea to fix this?

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Mar 14, 2018 at 12:44 PM

To first address the forum post you mention in the comment: The overhead at the start is mostly related to the fact that the coroutine scheduler most likely hasn't been initialized yet. However the overhead is completely neglectable "0.95 ms" for the first time is nothing. It's about 1/1000th of a second.


When you say "slow" you mean "slow over time", right? Have you checked that your "step" value is actually the same for two calls? Even fixedDeltaTime is usually a constant you should never read it only once and store it in a variable. Also you never really need to directly access fixedDeltaTime (except if you want to change the physics framerate). Time.deltaTime will return the fixedDeltaTime when used inside a Fixed update context. However be warned that your first iteration is not called within the physics loop as it's called immediately when you start the coroutine.


I don't see any reason why you would run this code in fixed update. It's not physics related at all so using fixedupdate will just make it look more choppy because it's not in sync with the visual update. Though if (for whatever reason) you want to keep it in fixedupdate do this:

 IEnumerator Move(GameObject go, Vector3 startPos, Vector3 endPos, float speed)
 {
     float step = (speed / (startPos - endPos).magnitude);
     float t = 0;
     var trans = go.transform;
     var waitFFU = new WaitForFixedUpdate();
     // make sure we are inside the fixed update cycle
     yield return new WaitForFixedUpdate(); 
     while (t <= 1.0f)
     {
         t += step * Time.deltaTime; 
         trans.position = Vector3.Lerp(startPos, endPos, t); 
         yield return waitFFU; 
     }
 }


If you want to run this every frame (which would be better), do this:


 IEnumerator Move(GameObject go, Vector3 startPos, Vector3 endPos, float speed)
 {
     float step = (speed / (startPos - endPos).magnitude);
     float t = 0;
     Transform trans = go.transform;
     while (t <= 1.0f)
     {
         t += step * Time.deltaTime; 
         trans.position = Vector3.Lerp(startPos, endPos, t); 
         yield return null; 
     }
 }
Comment
MrjmCyberman

People who like this

1 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 TreyH · Mar 14, 2018 at 02:36 PM 0
Share

Won't using Time.deltaTime within that first block with a yield instruction of WaitForFixedUpdate apply an incorrect time normalization?


Also, OP's block itself isn't explicitly calling physics operations, but OP might expect collider interactions during the movement of this object and need to use the FixedUpdate cycle for that reason.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

81 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 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 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 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 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

How to force Coroutine to finish 1 Answer

Coroutines and states 1 Answer

Why doesnt my coroutine ever end? 2 Answers

Coroutines IEnumerator not working as expected 2 Answers

understanding coroutines 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