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!

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.

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.