- Home /
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.
Answer by hexagonius · Feb 26, 2015 at 02:58 PM
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.
I know that, but I cannot figure out what to multiply it by in my example. I've done every variation possible, but when I go to quality and switch between simple and fantastic, the results are always different or not what was intended.
Your answer
Follow this Question
Related Questions
Movement using Time.deltaTime not working on Fast mac 3 Answers
Force mode and delta timing 1 Answer
(NEW USER) 2D character movement is laggy 0 Answers
RigidBody.MovePosition seems jerky in movement 0 Answers
move player to its rotating direction? 2 Answers