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.

2 Answers

2

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.

–

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][1] in FixedUpdate. Reading [Time.fixedDeltaTime][2] is specifically warned against. [1]: http://docs.unity3d.com/ScriptReference/Time-deltaTime.html [2]: http://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html

–

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.

–