Yet another Time.deltaTime question

Jumping and framerate independence. Tried solutions, didn’t like.

Here is simple example that demonstrates the point - empty scene, attached this to gameobject:

using UnityEngine;
using System.Collections;

public class jumpTest : MonoBehaviour
{
    private float velocityY = 0;
    private int counter = 0;
    private float jumpHeight = 0.1f;
    private Vector3 gravity = new Vector3(0,-.1f,0);

    // Update is called once per frame
	void Update ()
    {
       // gravity framerate independent
       float gravityY = gravity.y * Time.deltaTime;
        
       if (counter == 0)
        {
            // Jump is impulse, wont mult jumpHeight
            // @see http://answers.unity3d.com/questions/1156824/forcemode2dimpulse-do-we-need-timedeltatime.html
            // velocity framerate independent
            velocityY += (jumpHeight + gravityY);
        }
        else
        {
            velocityY += gravityY;
        }

        if (velocityY < 0)
        {
            velocityY = 0;
        }

        transform.position = new Vector3 (
             transform.position.x,
             transform.position.y + velocityY,
             transform.position.z
         );

        counter++;
    }
}

So what do we have here - first frame execution - add “upward impulse” to gameObject. future frames: applies gravity to the gameobject, so that at some point going up, the gravity takes over and gameObject starts falling down. As soon as we register falling down, we STOP! the simulation and check the Y position.

I expect if this was framerate-independent we would get approximately the same Y position from multiple runs (difference in +/- size of last deltaTime as the update ticks differ from run to run), However, this aint the case. The y-position differs wildly from run to run. Also on mobiles where framerate dips lower, Y-position is higher. Framerate-dependent. How do we make this piece of code FPS independent?

Here is the solution:

Change:

     transform.position = new Vector3 (
          transform.position.x,
          transform.position.y + velocityY,
          transform.position.z
      );

To:

         transform.position = new Vector3 (
              transform.position.x,
              transform.position.y + velocityY * Time.deltaTime,
              transform.position.z
          );

Actually doing what you did in your answer is what most people do. However it’s still not frame independent. The error is quite small but still there. For most singleplayer applications the error can be ignored. However if we talk about multiplayer you want to ensure that all players get the same values.

Here’s an example table of how time, acceleration, speed and position of an object advance depending on the way they are calculated. The assumed framerate is 10 frames per second for this example.

deltaTime(dt) = 0.1 (== 10fps)
time    acc     speed    pos1   pos2   pos3(old  + last + cur  == new)
0.0     2       0.0      0.0    0.0         0.0
0.1     2       0.2      0.02   0.02        0.0  + 0.0  + 0.01 == 0.01
0.2     2       0.4      0.06   0.04        0.01 + 0.01 + 0.02 == 0.04
0.3     2       0.6      0.12   0.09        0.04 + 0.02 + 0.03 == 0.09
0.4     2       0.8      0.2    0.16        0.09 + 0.03 + 0.04 == 0.16
0.5     2       1.0      0.3    0.25        0.16 + 0.04 + 0.05 == 0.25
0.6     2       1.2      0.42   0.36        0.25 + 0.05 + 0.06 == 0.36
0.7     2       1.4      0.56   0.49        0.36 + 0.06 + 0.07 == 0.49
0.8     2       1.6      0.72   0.64        0.49 + 0.07 + 0.08 == 0.64
0.9     2       1.8      0.9    0.81        0.64 + 0.08 + 0.09 == 0.81
1.0     2       2.0      1.1    1.0         0.81 + 0.09 + 0.1  == 1.0
1.1     2       2.2      1.32   1.21        1.0  + 0.1  + 0.11 == 1.21
1.2     2       2.4      1.56   1.44        1.21 + 0.11 + 0.12 == 1.44
1.3     2       2.6      1.82   1.69        1.44 + 0.12 + 0.13 == 1.69
1.4     2       2.8      2.1    1.96        1.69 + 0.13 + 0.14 == 1.96
1.5     2       3.0      2.4    2.25        1.96 + 0.14 + 0.15 == 2.25
1.6     2       3.2      2.72   2.56        2.25 + 0.15 + 0.16 == 2.56
1.7     2       3.4      3.06   2.89        2.56 + 0.16 + 0.17 == 2.89
1.8     2       3.6      3.42   3.24        2.89 + 0.17 + 0.18 == 3.24
1.9     2       3.8      3.8    3.61        3.24 + 0.18 + 0.19 == 3.61
2.0     2       4.0      4.18   4.0         3.61 + 0.19 + 0.2  == 4.0


[pos1] -- the usual way to calculate movement
speed = speed + acc * dt
pos = pos + speed * dt

[pos2] -- the correct values determined by absolute time passed (t)
speed = acc * t
pos = (acc/2) * t²

[pos3] -- the tricky way how to get correct values when calculating accumulative each frame
pos = pos + (speed/2)*dt
speed = speed + acc*dt
pos = pos + (speed/2)*dt

As you might remember from physics class this is how you calculate the position if you have a uniform acceleration. This value is shown in the table as [pos2]. This is not caculated accumulative as we have to do it. Since the real world doesn’t work in “frames” our splitting of the time into frames cause a lot of trouble. A linear change can be scaled by deltaTime without any problems since scaling by dt is a linear operation. However it doesn’t work for a quadratic progression since we can’t assume the values in between two points are linearly distributed which we do when we calculate the position as shown in [pos1].

The actual trick to get the correct answer independent from the framerate is to do this each frame:

  • add half of the last speed multiplied by “deltaTime” to pos.
  • update speed as usual by adding the acceleration multiplied by “deltaTime”.
  • once more add half of the current speed multiplied by “deltaTime” to pos.

So it would look like this:

Vector3 velocity = Vector3.zero;

void Update ()
{
    transform.position += velocity*Time.deltaTime / 2;
    velocity += gravity * Time.deltaTime;
    if (shouldJump)
        velocity += Vector3.up * jumpHeight;
    transform.position += velocity*Time.deltaTime / 2;
}

Note: adding a jump impule should be done only in one frame. This force should not be multiplied by Time.deltaTime. “shouldJump” is simply your condition when you want to perform a jump. This should be true for one frame.