- Home /
Does AddForce use Time.deltaTime or TIme.fixedDeltaTime or it depends on whether it is being used in Update() or fixedUpdate()?
I recently learnt how AddForce works. All was clear as the explanations were in terms of how the force is applied each frame and how Time.deltaTime is used to smoothen the force out to 1 second.
But then, I learnt that AddForce is a function of RigidBody and the physics engine handles forces on RigidBodies, so it is better to use AddForce function inside a FixedUpdate() and not an Update().
So how does it make sense to use Time.deltaTime inside a fixedUpdate? it would make sense if it was used inside an update and similarly, fixedDeltaTime was used inside fixedUpdate.
The documentation is pretty crappy about this and I learnt that is to keep the mathematics out of it to keep it simple. But then how do we know how it works?
Answer by Bunny83 · Jul 18, 2020 at 01:57 AM
Well it works pretty much as I have explained it over here. Though forces applied outside of FixedUpdate will only have an effect once FixedUpdate (or to be more precise once a physics update has happened) ran. So in my answer I said that the AddForce call is equal to the direct velocity change which is somewhat true for the overall effect. However when you call AddForce the velocity is not affected immediately. Forces are accumulated and applied during a physics update. Though conceptionally it makes no difference.
So yes, ForceMode.Force will actually use fixedDeltaTime internally. That's why using that force mode ouside of FixedUpdate doesn't make much sense.
About using AddForce outside FixedUpdate: It all boils down how it's used and which ForceMode. Any continuous forces have to be applied in FixedUpdate. However any one-time-event based things can be applied from whereever you like. Though you would never use ForceMode.Force in that case, especially since it wouldn't be a force in this case but an impulse.
Like Edy said you never use Time.deltaTime or fixedDeltaTime manually when using AddForce. In addition keep in mind that you generally shouldn't use fixedDeltaTime anywhere. Just use Time.deltaTime everywhere you actually need it. Time.deltaTime will actually return a different value depending on from where it's called. It usually returns the latest delta time. However when used inside FixedUpdate Time.deltaTime will return the value of fixedDeltaTime. This makes it easier to move code between Update and FixedUpdate.
Note that you shouldn't really change the fixedDeltaTime setting. We have the FixedUpdate to have a consistent update because many physics machanics can't be linearly scaled by deltaTime. Changing fixedDeltaTime will actually slightly change the behaviour of the physics. I explained that over here
What do you mean by not changing fixedDeltaTime setting? Is that the fixed timestep settings of the physics engine? Or dynamically changing it through script during runtime?
Answer by Edy · Jul 18, 2020 at 12:02 AM
Rules to use Rigidbody.AddForce:
Always from FixedUpdate.
Never multiply the force value by Time.deltaTime or fixedDeltaTime
A force is a time-independent magnitude. It's integrated along time internally by the physics engine to update the pose of the Rigidbody. Using the above rules guarantees your simulation to behave the same independently of the physics update rate (fixedDeltaTime) or the visual frame rate (deltaTime).
what if im using Rigidbody.AddForce in a function, and call it when certain key is pressed, does it act the same as FixedUpdate?
Any Rigidbody.AddForce() calls outside FixedUpdate() will be accumulated and all them will have effect the next FixedUpdate() cycle. However, note that multiple Update() calls may happen between each FixedUpdate() call. For example, if the key is detected as pressed during two consecutive Update() calls the result will be equivalent to applying twice the original force.