Is Time.deltaTime necessary for Rigidbodies?

I heard online that in newer versions of Unity (or maybe older ones too, not sure), you don’t require multiplying the vector3’ values by `Time.deltaTime` everytime you move an object using `Rigidbody.velocity` to prevent FPS from determining the speed of that object, instead of real time. Is that true or false? Or is it half-true, for example using `fixedUpdate()` instead of `Update()` makes so you don’t need `Time.deltaTime`?

Rigidbody.velocity is an absolute velocity in m/s. You don’t multiply it by deltaTime, just set the velocity value in the rigidbody. FixedUpdate should be used, but the resulting velocity would be the same if assigned from Update.

so if I do this:

``````void Update()
{
Rigidbody.velocity += 1f;
}
``````

it is the same as this…

``````void fixedUpdate()
{
Rigidbody.velocity += 1f;
}
``````

as well as this?

``````void Update()
{
Rigidbody.velocity += 1f * Time.deltaTime;
}
``````

So there is no need to ever use Time.deltaTime to deal with physics?

This isn’t a physics question really. You need to scale any change by the change in time if you want that change frame-rate independent.

Ignore physics. Get an int field. Add 1 to it each frame. Run it for exactly 10 seconds. It’ll display a value which is frame-rate dependent i.e. if your framerate is higher, you’ll end up with a larger value after 10 seconds. Doing the same in FixedUpdate and you’ll always get approximately the same value because there’ll be the same number of FixedUpdate calls in 10 seconds.

In other words, if you want a constant rate of change then you need to do it the same number of times over a specfied time period. If you can’t do that but instead are doing it per-frame (which is variable) then you can scale what you add by the change in time to make it consistent again.

2 Likes
``````    void Update()
{
Rigidbody.velocity += 1f * Time.deltaTime;
}
``````

This will increase velocity by 1 each second independent of the frame-rate. If you take away the time-scaling it’ll increase it by 1 each frame so it’ll change with frame-rate.

The point about physics is that it only runs after the fixed-update and you can have several Update (frame renders) inbetween that so the bigger question becomes one of do you really need to update it per-frame as it won’t be simulated per-frame.

3 Likes

Got it, thank you.

The AddForce method of a rigidbody needs to be scaled by Time.deltaTime or Time.FixedDeltaTime to be frame independent?

1 Like

Sorry, I cannot just say A or B. There isn’t a good simple answer because of the requirement to understand what you’re asking in more detail.

When you add a force once (say 100) then, during the simulation step, 100 won’t be added to the velocity even with a mass of 1 but instead 100 will be time-integrated first (scaled by the simulation delta-time then added). So if you do this at a constant rate and the simulation is run at a constant rate then you’ll get a constant acceleration.

What you’re asking is more complex because you’re asking (effectively) if I add 10 to the score per-frame, will it go up at a constant rate and of course the answer is no if the frame-rate is varying. If you want 10 to be added to the score per-second then of course you’ll need to add 10 * Time.deltaTime. Physics is no different. If you’re continually adding an acceleration then just adding it at a variable frame-rate means you’ll won’t get a constant acceleration. You need to scale it by the elapsed time.

Your question is asking about change of velocity AND what physics does with the value you pass when it integrates.

Often it’s said add-forces during FixedUpdate and the only reason is because then you’ll get a constant acceleration without having to scale the acceleration by elapsed time.

1 Like

Ok, I read the whole thread but still, as a newbie in Unity, I didn’t understand why Rigidbodies does not need Time.deltatime?
Isn’t physics rendered in frames?
What’s the general logic in this? Is it just have to be used in Fixedupdate (as it is updated 50 times)…

Probably mixing lots of things but trying to understand root of this.

Why Rigidbodies specifically? I’ll assume you mean the simulation as a whole.

Physics doesn’t render anything, it’s physics. It runs by default during the fixed-update although you can change that. For 2D you can change this in the 2D Physics Project settings to run during the FixedUpdate or per-frame (Update) or Manually.

The reason it runs during the fixed-update is for stabliity and consistency. Physics simulations are sensitive to varying time-steps which you get if you simulate per-frame. This is especially true when the time-steps are very large as the simulation suffers.

For example I have a 2D player character moved by Rigidbody component. Do I need Time.deltatime for getting better performance?

Don’t take this the wrong way but I cannot answer that because it doesn’t make sense as a question.

So…

In what way do you mean “need” it?

I’m just not sure where performance comes into this. Maybe the answer to the above will make it clearer. Maybe show some script as an example. Do you mean Rigidbody2D.MovePosition?

I’ll be honest, this is kind of hijacking this thread which was resolved. It would’ve been better to create your own.

Sorry but my intention was not to create a second, almost identical post with the same title. I will create another post with my question. Thanks.

The title being the same doesn’t mean the subject or the discussion really is. Far too many hijacked threads already with the net result being several different conversions on the same thread reused over the years. We are constantly pushing devs to create new threads but it’s pretty much an impossible task.