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 simplyRubbish · Mar 26, 2011 at 02:51 AM · yieldwaitforsecondsslowdown

yield return new WaitForSeconds is taking to long?

I'm using AngryAnt's behave and Starting my behave tree like this:

IEnumerator Start () { OriginalMaxSpeed = GetComponent<AutonomousVehicle>().MaxSpeed; m_Tree = BLNewBehaveLibrary0.InstantiateTree( BLNewBehaveLibrary0.TreeType.TrafficCollection_CivilianTrafficTree, this);

 while (Application.isPlaying &amp;&amp; m_Tree != null)
 {
     yield return new WaitForSeconds(1.0f / m_Tree.Frequency);
     AIUpdate();
 }

}

However using the Unity built-in profiler it's showing this is taking 14ms each time and this script is attached to to every vehicle since it's the behavior for vehicles.

Am I doing something wrong? Should WaitForSeconds be taking this long? What is an alternative to using WaitForSeconds()?

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
1

Answer by Bunny83 · Mar 26, 2011 at 03:21 AM

You can't get faster than your framerate. Coroutine "steps" are executed after each frame. The only way to get faster would be to create your own thread, but the Unity API is not thread save so you can't use anything from unity. You would still need to copy the required data inside update. Even FixedUpdate isn't running faster. It's not "Fix" it's "Fixed". That means if your FixedUpdate should run at 60 fps but your current Frame rate is 30 fps each frame Update is called once and FixedUpdate is called twice in a row. If the framerate is lower it will be called more often to fix the framerate statistically.

For most tasks FixedUpdate is enough. You have a constant call rate within a defined timeslice. But you can't rely on the time between the calls. Time.deltaTime will return a fix value inside FixedUpdate but it's not the true delta between the calls. It's just 1.0f/FixedRate

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 simplyRubbish · Mar 26, 2011 at 03:58 AM 0
Share

No, I don't want to execute yield faster, I want to figure out why yield is taking so long to execute Next$$anonymous$$ove. Is there a way to set the frequency in which yield executes?

avatar image loopyllama · Mar 26, 2011 at 05:41 AM 1
Share

Bunny is saying 14ms is your framerate and coroutines can be called at a maximum frequency of your framerate, and that is why it is "taking so long" for $$anonymous$$oveNext to be called. Coroutines are useful for making updates that are slower than your framerate, to save needless processing of selected code by updating less frequently. If you want $$anonymous$$oveNext to be called faster, optimize your entire application.

avatar image Bunny83 · Mar 26, 2011 at 12:22 PM 0
Share

yield don't "executes" and then runs on. yield completely breaks out of the current function and returns the control back to unity. It also saves the current position and states from inside the coroutine and returns the data as IEnumerator. When Unity have completed the next frame it will go through all saved coroutines and continue execution. If your frame time is 14ms (means around 70fps) you can only wait for multiple times of 14ms. If you use WaitForSeconds with 10ms it will take 14ms. With 15ms it will take around 28ms and for 29ms it would take 42ms.

avatar image Bunny83 · Mar 26, 2011 at 12:27 PM 0
Share

A Coroutine implements a cooperative multitasking system. Every coroutine have to decide when it stops execution. If you want more information on multitasking, maybe take a look at: http://en.wikipedia.org/wiki/Computer_multitasking

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

No one has followed this question yet.

Related Questions

no "pointers" to get directly to a variable passed to a function? 1 Answer

Nothing happening after WaitForSeconds C# 2 Answers

Why does Yield WaitForSeconds () only run once 1 Answer

Yield WaitForSeconds outside of time.Scale 7 Answers

C# WaitForSeconds doesn't seem to work ?? 2 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