# When to apply delta time for framerate independence.

I have been writing a bunch of physics related code for my parkour game lately. Before now I had been using all this code in FixedUpdate, but do to efficiency reasons I would now like to achieve framerate independence via the use of Time.deltaTime. The problem is whenever I use it, it never seems to work properly. An example piece of code I would like to use Time.deltaTime on would be:

``````using UnityEngine;

public class c_Physics
{
static public Vector3 Vector3_Spring(Vector3 current_vector, Vector3 target_vector, Vector3 last_delta, Vector3 stiffness, Vector3 damping)
{
if (target_vector == current_vector)
{
return Vector3.zero;
}
Vector3 distance = target_vector - current_vector;
Vector3 delta = Vector3.Scale(distance, stiffness / 100f) + last_delta;
delta -= Vector3.Scale(delta, damping / 100f);
return delta;

}
}
``````

// Next piece of code
using UnityEngine;

``````public class Vector3_Spring_Test : MonoBehaviour
{
Vector3 current_vector; // current location of the spring.
Vector3 target_vector; // where it needs to be.
Vector3 last_delta = new Vector3(0.0f, 0.0f, 0.0f); // How much force was addded to the spring last frame
Vector3 stiffness = new Vector3(1f, 1f, 1f); // how fast it aproaches target
Vector3 damping = new Vector3(0f, 0f, 0f); // how fast it stops moving

public void Update()
{
current_vector = transform.position;
target_vector = new Vector3 (0.0f, transform.position.y, transform.position.z);
last_delta = c_Physics.Vector3_Spring(current_vector, target_vector, last_delta, stiffness, damping);
transform.position += last_delta;
}

}
``````

No matter where I apply Time.deltaTime in this example I always end up with issues, such as the frequency of my spring will match across framerate, but the amplitude will not, or the spring will just snap to its origin without oscillating at all. Do to the large amount of physics and movement based math in my game I would really like to figure deltaTime out.

Think of deltaTime as of “per second“. Whatever you multiply it with happens per second. But only in Update, it’s not needed in FixedUpdate as long as you manipulate velocity for example, because that’s already per second by nature.