- Home /
Should we use Time.deltaTime in FixedUpdate
I know this question has been asked once http://answers.unity3d.com/questions/530478/is-it-even-necessary-to-multiply-by-timedeltatime.html but it looks as though it has not been settled. The first few answers only talk about how AddForce shouldn't have Time.deltaTime because it has it inherently. The last answer(as of now) by FrimaMickD says not to use Time.deltaTime inside FixedUpdate and it makes sense to me because no matter what computer, FixedUpdate will be called at the same rate all the time(as far as I know, quote me if I'm wrong) and Time.deltaTime is there to remedy problems that could occur inside the Update function because framerates may vary over time, and on different computers. However, I've seen official Unity tutorials using Time.deltaTime inside FixedUpdate and also in the comment section of FrimaMickD's answer it looks as though sb is suggesting that Time.deltaTime needs to be used inside FixedUpdate in certain cases. Can someone please resolve this issue it's been driving me crazy.
If you edit your post, and tell us what Unity tutorials use deltaTime
inside FixedUpdate
, I'll go find whoever wrote the code and get them to explain. I suspect it's just dodgy code.
I'll just right them here. 11:16 in http://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-player and the movement variable in http://unity3d.com/learn/tutorials/projects/survival-shooter/player-character
Answer by Kiwasi · Jan 08, 2015 at 01:50 AM
If you move a transform by modifying its position directly then use Time.deltaTime. If you want any time based effects use Time.deltaTime.
Don't use Time.deltaTime with forces, forces are not a time based effect.
This advice is true if you are using Update or FixedUpdate.
But I thought FixedUpdate had constant framerate on its own. Am I just wrong about that fact? because in which case just using FixedUpdate would take care of problems that my occur in time based effects
FixedUpdate has a constant frame rate, unless you modify it. You can modify it for several reasons
During development, to squeeze extra performance or accuracy out of the physics engine
At run time to match time scale changes
So putting Time.deltaTime into FixedUpdate insulates your code against any changes to the 'constant' frame rate.
Its also worth noting as indicated by @Graham Dunnett that normal practice is not to have any time dependent stuff in FixedUpdate. But if you do put it there, use Time.deltaTime.
Agree. For performance reason you may change fixedDeltaTime in settings, so all your values will be wrong if you don't use Time.deltaTime!
does multiplying by Time.deltaTime affect the size of the variable? Like Owen Reynolds's answer from http://answers.unity3d.com/questions/530478/is-it-even-necessary-to-multiply-by-timedeltatime.html ? And I get that we should use them when directly modifying an object's position, but should we use Time.deltaTime when using Forces and modifying Euler rotation too?
Answer by InvincibleCat · Jan 07, 2015 at 07:23 PM
Here is the answer: "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."
http://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html
So yes, use Time.deltaTime ;)
Cheers
Whether you use deltaTime or fixedDeltaTime rather depends on what for... they're two different things.
Well, all other purposes aside, I was just wondering if we should use Time.deltaTime inside FixedUpdate with intent to resolve the problem which occurs due to having different framerates in different computers. Like as we would multiply the Time.deltaTime function inside a variable inside the Update function in certain cases. I appreciate your answer but I since I don't seem to completely grasp your point I'd be grateful if you could elaborate on your point a bit more thankx s :)
fixedDeltaTime shouldn't be use to resolve the problem which occurs due to having different framerates in different computers. Always use Time.deltaTime :)
But do we have to worry about that kind of problem inside FixedUpdate? I thought since FixedUpdate will have its own fixed framerates we wouldn't really have to worry about it and use Time.deltaTime
Oh I see, no, you need it! It still uncertain so use it, it's a best practice
Answer by Graham-Dunnett · Jan 07, 2015 at 07:07 PM
Well, use Time.deltaTime
inside your FixedUpdate()
if you want to include how many seconds the previous frame took to render. There is a Time.fixedDeltaTime
which tells you the time between fixed updates. Since I've never seen any fixed update code that needs to know how long the previous frame took, I'd say don't use Time.deltaTime
inside a FixedUpdate()
.
So we don't need Time.deltaTime inside FixedUpdate if the purpose is to adjust the intervals like we would inside Update right? thanks
Answer by UpitSoft · Mar 31, 2021 at 05:51 AM
Time.deltaTime returns Time.fixedDeltaTime in FixedUpdate(). You can try it ;)
Answer by JustHallowed · Mar 18, 2021 at 03:07 AM
Offtopic but I think Time.fixedDeltaTime could be used for coroutines if it is used for some sort of physics calculation.
IEnumerator MoveTowardsWaypoint()
{
while(doLoop)
{
transform.position = transform.position + direction * Time.fixedDeltaTime;
yield return new WaitForFixedUpdate();
}
}
Your answer
Follow this Question
Related Questions
AddForce in Update 1 Answer
FixedUpdate vs. Update for instantaneous ForceMode's 1 Answer
Slow update best practice 0 Answers
DontDestroyOnLoad causing update to not work in next scene? 1 Answer