- Home /
Optimizing coroutines?
I have a bit of a problem.
I am trying to make this happen:
Have 100 slots in a circle and have another object move from slot to slot at a very high speed. (speed, or time in each slot varies through the game). If the object is in slot 45, it will wait a little moment (yield) and then move to 46, then 47, etc.
I have done this already, with a coroutine that checks the position of the object and sends it to the next position. However, when the times get really small (time interval between moving form slot a to slot b), the REAL speed at which the object moves (the speed you as a user see on the device) varies from device to device. For example, I ran this program on my computer and it took x secs to go through all the slots, however, on an android device, it took x/2 to move through all the slots...
I need it to be exactly the same on all devices. Any help will be much appreciated!
Thanks in advance!
ps: help can be, optimizing tips, or just other ways of doing the same thing. Thanks!
Can't you just include the coroutine? Or at least the important parts. "yield" could be used to yield for very different things. What do you yield? null? WaitForSeconds? ...
Answer by Bunny83 · Feb 17, 2013 at 02:44 AM
The easiest way in a coroutine to get the behaviour you want is to yield until the next FixedUpdate with WaitForFixedUpdate. This will ensure that the behaviour is the same on all platforms. The only exception is when you're FPS drops below the maximum allowed timestep. In this case you should try some performance optimizations.
Thanks Bunny83, I didn't know this, I'll give it a try. Thanks!
Answer by Alan47 · Feb 17, 2013 at 02:36 AM
I don't know about your entire setup and the environment your script is supposed to work in, but do you have to use a coroutine for this task? If there is something time-critical that should work the same on all devices, regardless of computational power, you should generally try to use either the "Update()" method and utilize Time.deltaTime or Time.smoothDeltaTime to calculate the position or use "FixedUpdate()". You can then also realize a "wait for object to be in slot X" like behaviour using coroutines like in this fragment:
while(/* insert object location condition here */){
yield Wait(1);
}
yield return;
If you choose to do this, please be aware that this is quite CPU intensive, especially if you have multiple copies of such coroutines going on at the same time. I used to work with coroutines extensively in the past, but I finally realized that the best way to use the Unity coroutines most often is to not use them at all.
Your answer
Follow this Question
Related Questions
yield, Coroutines, InvokeRepeating... 1 Answer
Are coroutines freezing my game? 0 Answers
How do I properly use Coroutines, yield and animators to sequence commands ? 2 Answers
Inconsistent StopCoroutine behavior with CustomYieldInstruction 1 Answer
Problem with Stopping Nested Coroutines: Control Never Returned to Parent 3 Answers