Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Time.smoothDeltaTime ???

Discussion in 'Scripting' started by DGuy, Apr 20, 2008.

  1. DGuy

    DGuy

    Joined:
    Feb 7, 2007
    Posts:
    187
    (This was asked in the WishList Topic, but I feel my reply would be more helpful, and more properly put, here.)

    Code (csharp):
    1.  
    2.   smoothDeltaTime = (current delta time + (sum of 'n' previous delta times)) / ('n' + 1)
    3.  
    Using smoothDeltaTime limits the effect of sudden fluctuations in deltaTime.

    Example:
    Code (csharp):
    1.  
    2.     deltaTime          smoothDeltaTime ('n' = 2)
    3.     --------           -------------------------
    4.     ...               ...
    5.     0.25               0.25
    6.     0.25               0.25
    7.     0.25               0.25
    8.     0.95 (+380%)       0.4833 (+193%)
    9.     0.25 (-73%)        0.4833
    10.     0.25               0.4833
    11.     0.25               0.25   (-51%)
    12.     0.25               0.25
    13.     ...                ...
    14.  
    In the table above, you see deltaTime is going along steadly at 0.25, but then suddenly it spikes up 380% to 0.95 then drop down 73% to 0.25. The visual effect of the spike would be everything suddenly moving 380% more units than the previous frame.

    The smoothDeltaTime, since the values are averaged over several frames, only sees a jump of 193%. And when it does drop back down to normal, the drop is only 51%. Now, while there will still probably be a visually noticible "hitch", it won't be as drastic as the deltaTime hitch. The hitch will have been spread, or "smoothed", out over several frames.

    I agree, it would be nice if some of the descriptions where expanded on, but hopefully my little description will help in the mean time (and is not too far off the actual mark)
     
    Suduckgames, senkal_, sean244 and 2 others like this.
  2. 5vdc

    5vdc

    Joined:
    Dec 14, 2010
    Posts:
    2
    Thank you for sharing.
    The documentation hasn't been updated yet :-/
     
  3. CgRobot

    CgRobot

    Joined:
    Jan 4, 2011
    Posts:
    175
    I didn't even know there was such a thing as smoothDeltaTime. Thanks for explaining this. I'm sure I will use this at some point.
     
  4. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    Just for information if any docs manager comes by this section :
    April 2012 (4 years later), and the docs about it are still vague. I had to Google search "smoothDeltaTime" to find out further explanations here.
     
    Last edited: Apr 9, 2012
  5. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    The doc is quite hurting in some parts. I wonder why they didn't make a wiki version where anyone could contribute to keep things updated.
     
    ModLunar likes this.
  6. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Cause the docs also have to be accessible offline and it doesn't work with Wikis? :p
     
  7. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    You can have manual for offline use + an online version and a separate wiki just for online where users can add things. Many applications use this sort of method and it works wonderfully.
     
    SparrowsNest likes this.
  8. Izitmee

    Izitmee

    Joined:
    Jan 27, 2011
    Posts:
    3,227
    Thanks DGuy for the useful information, and thanks n0mad for bumping this :)

    Unity docs should really be taken more seriously by the Unity guys. The amount of time that a Unity user has to lose to get information about things that should be well documented is absurd. Online searches, forum help, direct trial and error testing. And all this not to find solutions to issues, but just to understand undocumented (or badly documented) API. If we count these hours as working hours, Unity costs a LOT more than 1,500$.
     
    eneroth3 likes this.
  9. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,960
    Should it not be possible to have a community driven wiki then?
    Someone just have to set one up and I'm sure many will contribute to it.
     
    Last edited: Apr 10, 2012
    SparrowsNest likes this.
  10. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    646
    why can't they just add a comments section to the docs like they have on php.net?! everyone could add clarifications and examples and everyone on the planet would benefit greatly!
     
    ModLunar and SparrowsNest like this.
  11. Bezzy

    Bezzy

    Joined:
    Apr 1, 2009
    Posts:
    75
    This. All day, this.

    Not that the resources aren't great as they are, but having something a bit more expanded than this kinda (understandably) rushed doc note would be great.
     
    Naphier likes this.
  12. Essential

    Essential

    Joined:
    Sep 8, 2011
    Posts:
    265
    Ahh, thanks for the explanation. The current doc's explanation—"Smooths the delta time"—could mean a hundred different things.
     
    ModLunar likes this.
  13. maynoam

    maynoam

    Joined:
    Dec 24, 2014
    Posts:
    9
    From my experimentation Unity calculates it as a linear combination of current frame time and previous smoothed time like this:

    newSmoothDeltaTime = f*Time.deltaTime+(1-f)*prevSmoothDeltaTime

    Where they seem to use f=0.2 (In Unity 5 at least)

    In the end I had to implement it myself because Time.smoothDeltaTime seems to be updated also when the timeScale is zero causing it to be nearly zero after my game was unpaused.
     
  14. bitbutter

    bitbutter

    Joined:
    Jul 19, 2012
    Posts:
    60
    Wow. 7 years(!) later the docs page for this hasn't changed.
     
    ModLunar likes this.
  15. Lysander

    Lysander

    Joined:
    Feb 24, 2013
    Posts:
    1,497
    The documentation almost never has the exact math that goes into any of its built-in methods and members, so this is just one of a million different things that have some magic going on behind the scenes, and no reason (or at least, no more than normal) to update the documentation. Anyways, they've been more focused on those promotional videos that they call tutorials.
     
  16. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Smooth delta time does exactly what it says.If there is a significant jump in the framerate you're running, SmoothDelta time will report values closer to your targetFPS but weighted by the amount of frame loss. deltaTime will give you the actualy time difference.
     
  17. hdtnl

    hdtnl

    Joined:
    Jul 28, 2016
    Posts:
    5
    Or might the reason about silly of their document for a big community :D then you all are here
     
  18. Legion4

    Legion4

    Joined:
    Apr 18, 2017
    Posts:
    4
    2018 and still had to google it since the Docs weren't very helpful
     
    ModLunar likes this.
  19. sean244

    sean244

    Joined:
    Nov 4, 2017
    Posts:
    31
    So shouldn't we always use smoothDeltaTime rather than just deltaTime? Is it ever preferable to just use deltaTime?
     
    Last edited: Jun 29, 2018
  20. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,014
    Seems like a matter of preference. What should happen if someone's machine lags? Should it skip (deltatime) and be caught-up the next frame, or should it go into slow-mo for a few frames(SmoothDeltaTime)? I've never tried SmoothDeltaTime, though.
     
  21. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    4,250
    Using smoothdelta you will run slower in the spike frames so a slow computer will lose against a faster one in multiplayer
     
  22. psorger

    psorger

    Joined:
    Dec 6, 2016
    Posts:
    1
    Not what I was looking for exactly, but this kind of implements a simple low pass filter, effectively filtering the time step so it doesn't change so much each frame. Simple DSP (digital signal processing)

    This:
    newSmoothDeltaTime = f*Time.deltaTime+(1-f)*prevSmoothDeltaTime
    Where they seem to use f=0.2 (In Unity 5 at least)
    Implements a simple "one pole" filter with a "cutoff frequency (knee) coefficient" of 0.2.

    As you increase the number of 'previous delta times' the 'curve' of fps over frames gets smoother. Of course, to really do it correctly minimizing distortion you have to multiply those previous delta times by their own coefficients...
     
    senkal_ likes this.