This post is translation version of [Unity] 厳密なタイマーに基づき周期的に処理を実行する.
Introduction
Using Time.deltaTime in Update() is general implementation for producing the loop function.
But it's not always real precise, because Update() execute timing is depends on frame per second.
(I'm stacked with this problem at implementing BPM synchronization system. ;_;)
This post show up how to avoid this problem.
There are 2 methods I found:
- Trick with WaitForSeconds
- Using FixedUpdate
Trick with WaitForSeconds
StartCoroutine() with WaitForSeconds() is alternative for Time.deltaTime.
In common use, take seconds value to WaitForSeconds(). but you can use 1[ms] alternatively.
it makes faked fast infinitly loop. and then, watch the elapsed time with Time.time.
Don't use Time.deltaTime for avoid fps depending.
IEnumerator FuncCoroutine() {
while(true){
if (Time.time > this.elapsed) {
// Do something
this.elapsed = Time.time + timeOut;
}
yield return new WaitForSeconds((float)1/1000);
}
}
this method works as better as "Using FixedUpdate".
Using FixedUpdate
Copy and paste Time.deltaTime code to FixedUpdate().
but heavy FixedUpdate() is taking bad effect for collision.