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.

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.

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.