5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – DoubleDouble Jan 20 '16 at 23:19
0
\$\begingroup\$

In Unity Execution Order of Event Functions enter image description here

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.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Oxide Sep 18 '15 at 16:07
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ The logic to display the next frame is simply increasing the frame index and then changing the sprite property of the SpriteRenderer. \$\endgroup\$ – Oxide Sep 17 '15 at 20:17
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.