- Home /
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 && 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()?
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
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?
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.
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.
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