Why is physics2D calculation on fixedUpdate?

Physics2D is run on FixedUpdate, not Update, so using Time.deltaTime is ussless.
The problem is, I want to use Time.deltaTime, so my game can run on multiple FPS values.

image
What stops me, from pressing update? What advantage does FixedUpdate has over Update, that made the unity developers to make that the default option is FixedUpdate?

You can use Time.fixedDeltaTime in FixedUpdate method.

You shouldn’t be doing anything in FixedUpdate() unless it’s directly related to physics.

If you read the docs for Time.deltaTime, they explain pretty clearly what it returns when called from within FixedUpdate()

Finally, here is some timing diagram help:

Here is an abbreviated and better-annotated version of the above:

Two good discussions on Update() vs FixedUpdate() timing:

Issues with getting player movement / player shooting synchronized / aligned so that bullets are properly emitted from the player’s current location, not the previous frame position:

When used from within FixedUpdate, Time.deltaTime returns the fixed time step value (Time.fixedDeltaTime).

Simply use Time.deltaTime from within FixedUpdate normally. Your game will run correctly on multiple FPS values.

However, using Time.deltaTime incorrectly leads to inconsistent results on different FPS values. A typical case is including Time.deltaTime in the force calculations.

FixedUpdate provides consistent physics simulations regardless of the visual FPS values. That’s designed specifically for that. Physics run at a constant frame rate, so results are consistent. Visual udpate can then run at any FPS rate. Use Interpolation in rigidbodies to prevent visual jittering.

When running physics from Update, then the physics simulation depends on the visual frame rate. This easily leads to glitches, for example the character could pass through solid walls because the FPS rate dropped for a moment.

1 Like

Adding to the existing replies:

You can run it per-frame which is fine but you have to be careful because you won’t get a consistent, repeatable simulation, especially if your frame-rate is varying wildly. Also if your framerate dips low then the simulation time-step is high so it can become very inaccurate.

For a lot of games this doesn’t matter att all but for sensitive things like multiple joint set-ups, it can produce bad simulations with joints “exploding” etc.

For collision detection, using continuous collision detection will help when the time-step is too high but it’s relatively expensive if you over-use it and isn’t a good solution to low frame-rate siutations.

On that note, 2D physics also has sub-stepping so you can run per-frame but ask that it always use a fixed interval. This stops the situation where there’s a low frame-rate (large time-step) messing up physics as it’ll automatically split that large time-step up into multiple time-steps. This can be a tad more expensive but as with anything in engineering, it’s a compromise.

When you select “Update” then interpolation is automatically ignored on any Rigidbody2D because the Transforms on a GameObject for a Rigidbody2D are written per-frame along with physics callbacks being made.

Another obvious advantage is that it’s easier to read input and then perform physics actions as you don’t need to defer them to the FixedUpdate callback.

See more about sub-stepping etc here: Unity - Manual: Physics 2D reference