Question concerning Time.deltaTime

I have noticed that on different computers sometimes that certain functions in my game are faster than they should be, some of my equations relating to time.deltatime follows the formula (x * time.deltaTime / speedfactor). I was wondering if this is what is causing my problems? I was thinking it should be x * speedfactor * time.deltaTime.

Some of the problems i have experienced are scaling of my viewbob due to graphics level, despite having delta times in place for them, this has led me to think that it is my multiplication of the time.deltaTime that is causing these scaling errors. It should be uniform headbob for all framerates and quality levels.


	public void HeadBob()

		m_Phase += curSpeed * Mathf.Clamp(playerScript.getFatigue(), 0.65f, 1f) * Time.deltaTime;
		curBobOffset.x += Mathf.Sin(m_Phase * Mathf.PI)* curXScale / 1000;
		curBobOffset.y += Mathf.Sin(m_Phase * Mathf.PI*2)* curYScale / 1000;
		playerCamera.transform.localPosition = curBobOffset;

If you have a speed factor, so when it’s value is getting larger you want it to increase faster, you have to do:

x += Time.deltaTime * speedFactor;

However if you have a time period, for example you want to reach 1.0 after 4 seconds, you have to do:

duration = 4.0f; // 4 seconds
x += Time.deltaTime / duration;

Keep in mind that deltaTime can be used to compensate framerate differences in linear equations. If something follows a quadratic function it can’t be linearly scaled. The errors are usually not that large, but are still there. Here’s the most common example:

Usually you do something like this:

// our input variables
Vector3 dir; // normalized direction we want to move
float acceleration; // acceleration in m/s²
Vector3 velocity;  // current speed in m/s
Vector3 pos;    // current position in m

// inside Update / FixedUpdate
velocity += dir * acceleration * Time.deltaTime;
pos += velocity * Time.deltaTime;

When calculating the position you actually have Time.deltaTime squared since the translation follows a quadratic curve when you have a linear acceleration. This however will result in the wrong position after a certain time where the error depends on the framerate. The funny thing is that people with low framerates move faster than those with high framerates. The problem is that both the velocity and position are only changed at discrete time steps and not continuously. To solve that error for constant accelerated movement you have to do this;

pos += velocity * Time.deltaTime + 0.5f * dir * acceleration * Time.deltaTime * Time.deltaTime;
velocity += dir * acceleration * Time.deltaTime;

The same behaviour can be produced when you apply half of the acceleration before the position change and another half after.

velocity += 0.5f * dir * acceleration * Time.deltaTime;
pos += velocity * Time.deltaTime;
velocity += 0.5f * dir * acceleration * Time.deltaTime;

However that only works for constant accelerated movement. If you have drag, friction joints, collisions, … the result can’t really be 100% correct on all machines as the actual equations are far to complex to scale them linearly. Nevertheless Time.deltaTime helps to drastically reduce the error.

It depends on the FPS, how many times the Update function is called…

I have discovered my error,

         m_Phase += curSpeed * Mathf.Clamp(playerScript.getFatigue(), 0.65f, 1f) * Time.deltaTime;
         curBobOffset.x += Mathf.Sin(m_Phase * Mathf.PI)* curXScale * Time.deltaTime; // was originally divided by 1000, should be time.deltaTime!
         curBobOffset.y += Mathf.Sin(m_Phase * Mathf.PI*2)* curYScale * Time.deltaTime;
         playerCamera.transform.localPosition = curBobOffset;

Basically i should have multiplyed the products of the sin functions by deltatime to get the correct change in position per frame!