Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Physics Glitches with Time.fixedDeltaTime and Slow Motion

Discussion in 'Physics' started by slingy11, Dec 14, 2016.

  1. slingy11

    slingy11

    Joined:
    Jun 8, 2013
    Posts:
    8
    I've been working on a game in Unity, and I've come across some physics glitches when dealing with slow motion and scaling Time.timeSacle and Time.fixedDeltaTime.

    Check out this picture for reference: https://i.imgur.com/9pjcF34.png

    There's the four players, and the blue one is the one that I control. After killing the other players to win the match, I have the game pause for a second, then gradually speed back up to real time.

    First Gif: https://gfycat.com/SplendidExcellentBeaver

    For the one above, I only adjusted Time.timeScale and didn't touch Time.fixedDeltaTime from it's default value of 0.2. From not adjusting the fixed delta time, the physics are choppy. In an Update function, this part looks like this:

    Time.timeScale += RealDeltaTime * MatchEndTimeIncrease;

    Where real delta time is a calculated delta time from using Time.realtimeSinceStartup, and MatchEndTimeIncrease is a constant value for how fast it speeds up time.

    Second Gif: https://gfycat.com/HappySameIguana

    In this one I didn't gradually speed up time, but just set it back to real time after the pause.

    Third Gif: https://gfycat.com/EminentEnviousAsp

    In this one I sped up time like the first gif, and also accounted for Time.fixedDeltaTime, looking like this:

    Time.timeScale += RealDeltaTime * MatchEndTimeIncrease;
    Time.fixedDeltaTime = Time.timeScale * 0.02f;

    This one's much smoother, but there's a problem. If you compare the little triangle parts that come out of the last player killed to the first two gifs, they don't fly back at all unlike in the other gifs.

    Fourth Gif: https://gfycat.com/MajorResponsibleBagworm

    This one's got the exact same code as the third gif, but the little triangle parts from the other players randomly fly upwards, which is some kind of glitch as well.

    What could I do to avoid these glitches? I don't want the game to look choppy, and I don't want physics glitches getting in the way on random occasion.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,430
    You shouldn't modify Time.fixedDeltaTime. This affects the entire physics system causing unexpected (and undesired) effects.

    Modifying Time.timeScale from Update is the correct method. Additionally, I'd recommend you to use Time.unscaledTime and/or Time.unscaledDeltaTime for time-independent operations instead of realtimeSinceStartup. These are equivalent to Time.time and Time.deltaTime but they're not affected by timeScale.

    The physics being choppy is solved by setting Rigdbody.interpolation to Interpolate in the rigidbodies.
     
  3. slingy11

    slingy11

    Joined:
    Jun 8, 2013
    Posts:
    8
    Thanks man, it's looking smooth now. It's also good to know there's an easier way of getting the delta time without Time.realtimeSinceStartup.
     
    Edy likes this.
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,430
    Yes, I've also discovered the unscaled time recently. Surprisingly, it has been available since Unity 4.5.
     
  5. DenisK_WRG

    DenisK_WRG

    Joined:
    Oct 16, 2018
    Posts:
    14
    And still reproduced in 2018.3 at least