Since coroutines run at a variable frame rate, how would you move a physics object inside a coroutine?
variable time delta? maybe, but on the iphone frame rates can have huge deltas
I’m trying to move the objects with a simulated time step; so far, working ok. Let me know your thoughts, maybe there is a better way.
using UnityEngine;
using System.Collections;
public class SimFixedTime : MonoBehaviour {
void Start () {
StartCoroutine(Simulate());
}
IEnumerator Simulate () {
bool done = false;
float accumulator = 0;
int fpsFixedSimulated = 0;
float frameTime = 0;
while (!done) {
accumulator += Time.deltaTime;
if (accumulator > Time.fixedDeltaTime) {
accumulator -= Time.fixedDeltaTime;
++fpsFixedSimulated;
// move physics here
}
// optional, for debug
frameTime += Time.deltaTime;
if (frameTime > 1) {
Debug.Log("Simulated fixed fps: " + fpsFixedSimulated.ToString());
frameTime -= 1;
fpsFixedSimulated = 0;
}
// end optional
yield return 0;
}
}
}