# 2D Animation based on Time.deltaTime has problems when the game performs long operations

I animate my 2D sprite using the Update() method:

float timer = 0.0f;
float delayPerFrame = 0.04f;

void Update() {
timer += Time.deltaTime;
if (timer >= delayPerFrame) {
timer = 0;
// Logic to display the next frame
}
}


As you can see, it is pretty simple: every 0.04 seconds the next frame will be set.

But sometimes, my game needs to do some exhausting processing (like loading large scenes and whatnot), which causes a tiny little lag in the game. When this happens, my sprite begins with a wild, fast animation. Then eventually it resumes at a normal pace.

I imagine this is because the lag caused the Time.deltaTime value to skyrocket for a period of time. I am also under the impression that Update() is called too often during such period (although that shouldn't matter).

I don't want to use existing animation features of Unity. My game is already built under this particular piece of logic, so I'd like to fix it.

Using FixedUpdate() doesn't help. I can also confirm that Time.deltaTime does indeed peak at the beginning.

Note that this occurs only after doing an exhaustive operation. If you spawn a sprite of these in the middle of the game, everything will go smoothly. But if you spawn them immediately after doing something like loading a large scene, then they will have some sort of boost at the beginning.

• Is there a loop within your "Logic to display the next frame" ? (also, did you figure this out? - You can answer your own question if you did) – DoubleDouble Jan 20 '16 at 23:19

I see that "Internal Animation Update" comes after all Update stuf, but before LateUpdate. As you manage animation on your own, try putting all animation logic (and only animation logic) into LateUpdate.

• No difference. I think it there will be a LateUpdate() for every Update(), and the delta will remain the same, so at the end there is virtually no difference. – Oxide Sep 18 '15 at 16:07

The Time.deltaTime is the amount of time elapsed between two consecutive Updates() calls. When the game tries to load a heavy asset , the update doesn't get called often as the process is stuck doing some task. Hence there is a lag between two Updates. Due to which the Time.deltaTime increases , which signifies that the system is lagging and not accelerating.

So it all boils down to how you are using Time.delta time. The problem lies in the logic to display the next frame.

• The logic to display the next frame is simply increasing the frame index and then changing the sprite property of the SpriteRenderer. – Oxide Sep 17 '15 at 20:17

as workaround, if nobody figures out what is the problem, you can , after loading a schene, fade it in until the sprites starts animating the right way.

If you are rolling your own sprite animations you may need to track elapsed time so that you can control how long each frame is displayed.

A common approach is to use an accumulator e.g.:

float animationFramesPerSecond = 15;
float animationFrameDuration = 60f / animationFramesPerSecond;
float animationFrameIndex = 0;

float accumulator = 0;

void Update()
{
accumulator += Time.time;
while (accumulator <= animationFrameDuration)
{
animationFrameIndex++;
accumulator -= animationFrameDuration;
}
}


An additional consideration might be to identify any heavy weight tasks that are taking too long to execute and splitting them up over a more than one frame. Frequently coroutines can be used for this purpose.