Search Unity

What exact value does Time.deltaTime supply?

Discussion in 'Scripting' started by Multiplayer, Dec 29, 2012.

  1. Multiplayer

    Multiplayer

    Joined:
    Dec 29, 2012
    Posts:
    15
    I am trying to make my games framerate independent and of course i was referred to Time.deltaTime.

    What I understand is that deltaTime will give me the time that the last render has taken, therefore making me able to adjust changes based on the time passed.

    What i need to know though, is if this "passed time" is actual, real world time or physics simulation time or maybe even the amount of expected update cycles per second or any variation of these.

    Here is why i do not understand it / need to know more about this:

    People always describe that 60fps would give you the same movement speed as 200fps if the fps fluctuate. Thats fine.
    But as a simple example, imagine a game where you are a bird and you may flap your wings only once every second. To make that one second regardless of my target platform (might be 60fps or 120 for example), I would use deltaTime.
    Now you open up some CPU intensive program in the background and your fps plummet to 1fps. Of course when I can only display 1 frame each second, i expect the physics and everything to run slower as well, or else it would turn merely annoying stuttering into unplayable game jumps, as i cannot be expected to react to a game world that runs at "real time" while I can see only one frame per second of it.

    If deltaTime delivers "The time in seconds it took to complete the last frame", as stated in the documentation, would that not mean that my bird can now flap its wings every frame, which might be only a fragment of a second in physics time? Would that not allow me to cheat by slowing down my computer? And would that not break any game most of the time when the framerate drops below playable values?


    While writing this I realized what I would like to get from deltaTime is the amount of physically simulated time since the last call to update. So is it that what it gives me, or is it the actual time since the last render, which would be kind of strange for the reason described above?
     
  2. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Time.deltaTime gives you the time in seconds since last update.

    E.g 60fps ~ 0.016, 30fps ~ 0.033.

    Sample use case: position += speed * Time.deltaTime.

    The problem with your reasoning is that you assume a game that is running at 1fps should be playable.
     
    Last edited: Dec 29, 2012
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, because of Maximum Allowed Timestep in the time manager. But yeah, nobody's going to play a game running at 1fps.

    --Eric
     
  4. Multiplayer

    Multiplayer

    Joined:
    Dec 29, 2012
    Posts:
    15
    I don't think it should be playable, I just think it should not allow me to cheat by making the fps drop :)
    The Maximum Allowed Timestep sounds like something I should look into though, thanks.

    Concerning npsf3000s comment "The problem with your reasoning is that you assume a game that is running at 1fps should be playable", does this mean that what is happening is that the game logik will not slow down when the fps drop but instead it will simulate a full second in one frame?
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, the Maximum Allowed Timestep setting will make the physics simulation slow down.

    --Eric
     
  6. Multiplayer

    Multiplayer

    Joined:
    Dec 29, 2012
    Posts:
    15
    OK then, thanks.
     
  7. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830