For various reasons in my current game, I do this quite often:
void Update () {
myPosition = Vector3.Lerp(myPosition, Vector3.zero, Time.deltaTime);
}
I’ve seen examples of this everywhere, and it appears to provide decent framerate protection.
However, as the following code illustrates, that approach is not perfect.
20 frames @ 60fps
hold = 100f;
for (int count = 0; count < 20; count++) {
hold = Mathf.Lerp(hold, 0f, (1f / 60f));
}
// outputs 0-98.33 1-96.69 2-95.08 3-93.50 4-91.94 5-90.41 6-88.90 … 19-71.45
10 frames @ 30fps
hold = 100f;
for (int count = 0; count < 10; count++) {
hold = Mathf.Lerp(hold, 0f, (1f / 30f));
}
// outputs 0-96.67 1-93.44 2-90.33 3-87.32 … 9-71.25
After 1/3 of a second:
60fps changes 100 into 71.45
30fps changes 100 into 71.25
although i’m more concerned about the difference at the start (98.33 vs. 96.67)
My game appears to play fine at 30fps, 60fps, and 200fps so I think this approach is good enough.
Is there a situation where this becomes a major problem?
Note: I have LOTS of camera code that will experience this same problem:
cameraPosition = Vector3.Lerp(cameraPosition, playerPosition, Time.deltaTime);
I imagine everyone here has similar camera code… because how else could you do it?
I’m hoping the error is so small it’s considered insignificant. But is that true?
I realize the perfect fix is to do this:
myPosition = Vector3.Lerp(myStartPoint, Vector3.zero, Time.deltaTime);
BUT that would require significant effort at this point,
determining myStartPoint is tricky in some cases,
and I have no idea how you’d apply this to a camera.