I want to pause my coroutines for some seconds, using fixed update for maximum accuracy. The code below works fine but is too verbose:
var endTime = Time.time + pauseDuration;
while(endTime > Time.time) {
yield return new WaitForFixedUpdate();
}
Ideally this would be a YieldInstruction, so I could just write yield return new WaitForFixedSeconds(pauseDuration)
. However I’m not sure how to force an IEnumerator implementation to execute in the fixed-update scheduler. I’ve tried this to no avail:
class WaitForFixedSeconds : IEnumerator {
public object Current { get { return new WaitForFixedUpdate(); } }
public bool MoveNext() { return Time.time > endTime; }
public void Reset() {}
public WaitForFixedSeconds(float delay) {
endTime = Time.time + delay;
}
float endTime;
}
How can I write a WaitForFixedSeconds YieldInstruction?