Search Unity

Time.deltaTime compensation?

Discussion in 'Scripting' started by Wasiim, Jun 11, 2015.

  1. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    I feel as though i do not know enough about Time.deltaTime sure i understand it utilizes seconds instead of frames to create consistency because each frame vary in completion time. I also understand that without Time.deltaTime rendering will be dominated by how much fps your hardware can provide so if you have code to move a object 3m/frame(basically no Time.deltaTime) the greater the fps the farther that object will go.

    What i dont understand is what is happening behind the scenes lets say i have code that says a object will move 50m*Time.deltaTime so i know that it is going to move 50m a second but what i do not know is what is
    going on in the frames what distance is that object moving? how is it using the completion time of the previous frame to find consistency?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Time.deltaTime is the time in seconds since the previous frame; that's it, nothing behind the scenes. If you got exactly 2fps consistently, then Time.deltaTime would be 0.5. If you got exactly 100fps, then Time.deltaTime would be 0.01.

    --Eric
     
  3. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    Ok i got it now so it changes the amount it moves each frame based on the fps you are getting so if the code is 50m*time.deltaTime and you are getting 2fps it will move 25m/frame and if you are getting 100fps its going to move 0.5m/frame. So the 2fps and 100fps are both moving equal distance.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep, exactly.

    --Eric
     
    Wasiim likes this.
  5. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    So one more thing what if you are doing 50fps and you drop to 20fps how would it calculate it so its 50m/sec
     
  6. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    50m/s at 50fps is still 50m/s (speed is 1m/frame or 50*Time.deltaTime, because deltaTime = 0.02)
    50m/s at 20fps is still 50m/s (speed is 2.5m/frame or 50*Time.deltaTime, because deltaTime = 0.05)

    So if you want to move something at 50m/s it's always 50*Time.deltaTime every frame, regardless of framerate.

    Time.deltaTime = 1 / Your Current FPS