Search Unity

Time.realDeltaTime

Discussion in 'Wish List' started by Talzor, Jan 31, 2007.

  1. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    It would be really nice if the Time class contained the real time it took to complete the last frame (i.e. not dependent on timeScale) and likewise that there was a WaitForRealSeconds(). This would make it a lot easier to run certain behaviors when the game is paused.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Neat idea actually.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Time.deltaTime / Time.timeScale?

    It'd be slightly more convenient I guess....
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Dividing by timeScale would break if you have set it to zero to pause the game.
     
  5. David-Helgason

    David-Helgason

    Moderator

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    A current workaround would be to store and compare this at the beginning of each frame. This could be wrapped into the following utility. If not called every frame, it'll approximate the realtime per frame by dividing by the number of frames passed.

    (edit: fixed to actually work)

    Code (csharp):
    1. // File: MyTime
    2. // Usage: var realtime = MyTime.RealDeltaTime();
    3.  
    4. private static var lastFrameCalled = 0;
    5. private static var lastTimeSinceStartup = 0.0;
    6. private static var currentDeltaTime = 0.0;
    7.  
    8. static function RealDeltaTime() {
    9.    if (lastFrameCalled != Time.frameCount) {
    10.       currentDeltaTime = (Time.realtimeSinceStartup - lastTimeSinceStartup)
    11.                              / (Time.frameCount - lastFrameCalled);
    12.       lastFrameCalled = Time.frameCount;
    13.       lastTimeSinceStartup = Time.realtimeSinceStartup;
    14.    }
    15.    return currentDeltaTime;
    16. }
    17.  
    d.
     
  6. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    This is a very useful method David!

    Any reason why it's buggy on Windows only? Seems that it doesn't get called; neither Windows Standalone nor Webplayer.

    Thanks for this,

    joe
     
  7. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    What is not called? The function in this script is not called by itself, you need to call it to return the "realtime" delta time.
     
  8. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    Sorry for leaving out the details... i'm using it properly as it works great on OSX.

    The same build made for windows, it is not working, as mentioned. It behaves as if it's not called at all.

    Haven't tried it with a dummy project so maybe it's something else in my code, but my use of it is pretty straightforward.

    Looking at it, I cannot tell why it would work on OSX, but the same code doesn't work on Windows.
     
  9. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    Caching this to a local var seemed to help on Windows.