- Home /
Framerate Dependency
Hey UnityAnswers,
I'm making a very simple 2D side-scroller for the learning experience. It does not use gravity or any other physics, the character just moves to the right. I keep reading about framerate dependacy and fixedupdate and what not and after reading a couple of UnityAnswers questions some of them seem to be contradictory(no offense).
Most people say that if you say FixedUpdate for your movement it's already frame rate independent and using DeltaTime is useless.
This is the code I have at the moment for my very basic movement: function FixedUpdate() { rigidbody2D.velocity = transform.right * 3; }
Will it function the same regardless of framerates? Please provide arguments and/or examples to help me understand.
Thank you in advance.
Answer by Kiwasi · Jul 18, 2014 at 11:03 PM
FixedUpdate runs at a fixed framerate, so it appears to be framerate independent. However there are many circumstances where you will want to change the physics framerate. Using Time.deltaTime will also make your calculations easier, as you will be working in per seconds instead of per fixedupdate frame. It also means there are no strange behaviours if you change your physics frame rate at a later stage.
Time.deltaTime will return according to the framerate of whichever update cycle you call it in. If you are in fixedupdate it will return the delta time from the last physics update, in update it returns the delta time from the last update cycle.
From the definition of FixedUpdate() not sure how it is frame rate independent. If it's called X number of times per frame that makes it frame rate dependent.
It is framerate independent. It is not called X number of times per frame but X number of times per second. By default the fixedDeltaTime is set to 0.02 seconds which makes FixedUpdate run exactly 50 times per second regardless of the visual framerate.
What makes the concept of FixedUpdate a bit hard to understand is the "realtime spacing" between FixedUpdate calls might not be equally, however that doesn't matter for physics since all we need is a constant call rate per second.
Answer by Saad_Khawaja · Jul 18, 2014 at 10:46 PM
Your code is not framerate/timescale independent yet.
Googled this answer for you:
Time.deltatime (duration of last frame) should only be used in once per frame calls e.g. Update() calls. Time.fixedDeltaTime should only be used in FixedUpdate(), since it the sample time of the physics loop.
The physics loop is called a fixed number of times per second (constant fixedDeltaTime) and Update is called a variable number of times per second (a non-fixed deltaTime). So, yes, you make calculations time independent in the Update-call and the FixedUpdate() by using Time.deltaTime and Time.fixedDeltaTime, respectively.
Edit: Just checked, Time.fixedDeltaTime returned 0.02 so that means FixedUpdate is called after every 0.02 seconds. You need to multiply with this variable to make sure that once you change timescale, your speed is constant.
Otherwise, it always gives the same number so you may/may not use it in your calculations. Your frame rate will be independent.
Check out the docs. No problem using Time.deltaTime in FixedUpdate. Reading Time.fixedDeltaTime is specifically warned against.
It is not warned against. If you are inside the FixedUpdate function, there is no difference between Time.deltaTime and Time.fixedDeltaTime.
Time.deltaTime is only recommended because it works on both Update and FixedUpdate. So if you know you're in FixedUpdate like the OP, I don't see any problem in using Time.fixedDeltaTime
Except three months down the track when you decide you want it in Update after all. Using Time.deltaTime will work in both settings.
Your answer
Follow this Question
Related Questions
When Unity3D finally supports WebGL? 2 Answers
Incrimenting a variable when in a certain area 1 Answer
error with learning script 2 Answers
How can I instantiate an object without using Unity? 1 Answer