- Home /
How to make code in FixedUpdate() independent from fixed timestep?
If you put a physics code into FixedUpdate(), e.g. rigidbody.AddForce(force)
and your fixed timestep is set to default 0.02 (50 FPS) then your code will be executed 50 times per second. But if you decide to change fixed timestep to 0.01 (100 FPS) the same force will be applied 100 times a sec so it'll have effect at gameplay.
If this happened mid-development, it could break the game and the only way to fix it would be to figure-out all the force values (in the inspector) yet again.
Is there a way to make physics calculation in FixedUpdate() independent from fixed timestep? Similar thing can be done for Update() by using Time.deltaTime but it doesn't seem to work in FixedUpdate().
Answer by Commoble · Feb 14, 2018 at 06:11 PM
FORCE_PER_SECOND = 50; // or whatever
rigidbody.AddForce(FORCE_PER_SECOND * Time.deltaTime);
// when used inside the FixedUpdate, Time.deltaTime is equivalent to Time.fixedDeltaTime
This is my method for applying force:
It makes a cube hover nicely above the ground. In play mode, when I change the timestep from 0.01 to 0.1, the cube starts jumping high ins$$anonymous$$d of hovering.
As @Commoble was saying, you can either call your ApplyForce() method inside FixedUpdate, or you can use Time.fixedDeltaTime.
But how does it solve the problem? If I put my physics code in FixedUpdate() or use Time.fixedDeltaTime and then change fixed timestep, physics will get broken. The question I asked was how to make code in FixedUpdate() independent from fixed timestep.