- Home /
Should you use Time.deltaTime in FixedUpdate()?
I've seen many different answers online and all of them never giving a full reason on way you should or should not. The question is in thee title, Should you use Time.deltaTime in FixedUpdate().
Answer by lcplrice · Dec 16, 2020 at 04:05 AM
Time.deltaTime is the time between last frame. Inside FixedUpdate() this can sometimes cause odd behavior depending on the load running in any given frame and give unexpected results. Though I have never seen this. lol.
Unity recommends using Time.fixedDeltaTime in FixedUpdate() to help keep the behavior consistent.
https://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html
https://docs.unity3d.com/ScriptReference/Time-deltaTime.html
Thanks for answering so quickly this was really informative! :)
No problem, I was in the right place at the right time. :)
just FYI. in most cases you will not needed in fixed update because fixedupdate is "fixed" therefore i += 1 / i++ would work just fine.
This answer is plainly wrong. Unity explicitly recommends using Time.deltaTime within FixedUpdate:
https://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html
> For reading the delta time it is recommended to use Time.deltaTime instead because it automatically returns the right delta time if you are inside a FixedUpdate function or Update function.
Answer by Edy · Dec 16, 2020 at 10:35 AM
You should use Time.deltaTime everywhere, unless you need to consult the actual physics update rate. Time.deltaTime returns the proper value depending where are you reading it from.
From https://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html :
> For reading the delta time it is recommended to use Time.deltaTime instead because it automatically returns the right delta time if you are inside a FixedUpdate function or Update function.
There is no actual advantage over using Time.deltaTime over Time.fixedDeltaTime but to keep it simple for people who dont really understand the difference between Update and FixedUpdate, that unity recomendation is only for beginners
It's a general recommendation to all Unity users. Beginners should adhere to it so they don't use Time.fixedDeltaTime incorrectly. Just use deltaTime and everything will work as expected, anywhere.
The main advantage is that calculations that use the delta time will run correctly from either Update or FixedUpdate without changes.
Your answer states that using Time.deltaTime instead of Time.fixedDeltaTime from FixedUpdate can cause odd behavior depending on the load. That's simply incorrect. Can you justify it?
Justify what? The accepted answer from the other guy? he is incorrect saying that it can cause odd behaviour. I just pointed out that deltaTime and fixedDeltaTime inside the FixedUpdate will always return the same value
its only been 2 days and Unity already says your link is broken :/
Fixed. Stupid auto-link feature using the trailing colon as part of the url...
this answer should labeled "Best answer", it says everything
Answer by shieldgenerator7 · Dec 18, 2020 at 10:29 PM
After reading @Edy 's answer, I had to do some tests. Here's the results:
(AvC is a poor man's outlier elimination: it removes the smallest and largest values, then averages the remaining values)
As you can see, deltaTime
fluxuates in Update()
but always returns 0.02
in FixedUpdate()
. Whereas fixedDeltaTime
always returns 0.02
no matter where it's called. This leads me to believe that fixedDeltaTime
is a setting rather than a reading of how long the last frame took.
Based on these results, it looks like you can use either one in FixedUpdate()
, although Unity does recommend using deltaTime
there too.
But most definitely, you should use deltaTime
in Update()
Yes fixeddeltatime is a value that you can modify from the project settings window