- Home /
Making FixedUpdate fixed-timestep-independent
In the online Unity documentation, it's recommended that you multiply by Time.deltaTime in order to make your game's logic framerate-independent. However, the FixedUpdate documentation contains examples such as the following:
function FixedUpdate(){
rigidbody.AddForce(Vector3.up);
}
This is typically okay since the rate at which FixedUpdate is called is constant. However, it becomes a problem if, after writing all your logic, you suddenly want to change the Fixed Timestep. In the above example, this would change the amount of time the specified force was being applied to the rigidbody (unless I'm misunderstanding how AddForce works).
Is it considered better practice to do something like the following (assuming the above code was written with a Fixed Timestep of 0.02)? Or is changing the Fixed Timestep considered a rare enough occurrence that logic like this is (normally) unnecessary?
var expectedTimestep = 0.02;
function FixedUpdate(){ rigidbody.AddForce(Vector3.up * (expectedTimestep / Time.deltaTime)); }
Answer by jonas-echterhoff · Feb 20, 2010 at 08:58 AM
The physics engine takes the elapsed time into account, so in most cases, it should not matter to you. Typically, forces are applied continuously (such as the engine of a car) - the same force is being applied over a long time.
The only case where it does make a difference is when you want to apply a force in a single frame, as an impulse - for example to manually apply the result of a collision. Then you should indeed take the time into account for consistent behavior at different fixed time step intervals.
Answer by Aubrey Hesselgren · Sep 20, 2010 at 02:27 PM
Also, use Time.fixedDeltaTime
for the time step between consecutive FixedUpdate
calls. That is the way to access your current fixed timestep setting in the manager.
Or have I misinterpreted the problem? Something to do with issues of synchronization between update and fixedupdate?
Answer by angularsen · Apr 02, 2018 at 03:13 PM
Detailed answer here: http://answers.unity.com/answers/1488426/view.html
TL;DR You do NOT need to multiply by deltaTime to get timestep independent physics.
Your answer
Follow this Question
Related Questions
"Fast" fixed timestep 1 Answer
How to make code in FixedUpdate() independent from fixed timestep? 1 Answer
Raycast drags behind the transform that generates it 1 Answer
Interpolating positions between FixedUpdate frames? 1 Answer
Physics behaviour changing with framerate - Is FixedUpdate() actually working properly? 3 Answers