Search Unity

Bug fixedUnscaledDeltaTime and FixedUpdate calls incorrect after timeScale change

Discussion in 'Editor & General Support' started by Rudy_LPN, Mar 30, 2023.

  1. Rudy_LPN

    Rudy_LPN

    Joined:
    Jan 3, 2023
    Posts:
    2
    On the first FixedUpdate after changing Time.timeScale, Time.fixedUnscaledDeltaTime seems to be incorrect. This can even cause FixedUpdate to be called more time than it should.
    An empty project with this simple script on a GameObject reproduce the issue.
    Code (CSharp):
    1. public class TimeTests : MonoBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         StartCoroutine(ReduceTimeScale());
    6.     }
    7.  
    8.     private IEnumerator ReduceTimeScale()
    9.     {
    10.         yield return new WaitForSecondsRealtime(2);
    11.         Debug.Log("Update time scale");
    12.         Time.timeScale /= 10;
    13.         Time.fixedDeltaTime /= 10;
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         Thread.Sleep(33);
    19.         Debug.Log($"[UPDATE] Frame {Time.frameCount}, Unscaled delta time: {Time.unscaledDeltaTime}, Delta time: {Time.deltaTime}, Unscaled time: {Time.unscaledTime}, Time: {Time.time}, TimeScale: {Time.timeScale}");
    20.     }
    21.  
    22.     private void FixedUpdate()
    23.     {
    24.         Debug.Log($"[FIXED_UPDATE] Frame {Time.frameCount}, Fixed unscaled delta time: {Time.fixedUnscaledDeltaTime}, Fixed delta time: {Time.fixedDeltaTime}, Fixed unscaled time: {Time.fixedUnscaledTime}, Fixed time: {Time.fixedTime}, TimeScale: {Time.timeScale}");
    25.     }
    26. }
    This procude the following logs:

    [UPDATE] Frame 22, Unscaled delta time: 0,0421071, Delta time: 0,0421071, Unscaled time: 6,102326, Time: 1,17877, TimeScale: 1
    [FIXED_UPDATE] Frame 23, Fixed unscaled delta time: 0,02, Fixed delta time: 0,02, Fixed unscaled time: 6,103556, Fixed time: 1,18, TimeScale: 1
    [FIXED_UPDATE] Frame 23, Fixed unscaled delta time: 0,02, Fixed delta time: 0,02, Fixed unscaled time: 6,123556, Fixed time: 1,2, TimeScale: 1
    [UPDATE] Frame 23, Unscaled delta time: 0,0374097, Delta time: 0,0374097, Unscaled time: 6,139736, Time: 1,21618, TimeScale: 1
    Update time scale
    [FIXED_UPDATE] Frame 24, Fixed unscaled delta time: -0,1256213, Fixed delta time: 0,002, Fixed unscaled time: 5,997935, Fixed time: 1,202, TimeScale: 0,1
    [FIXED_UPDATE] Frame 24, Fixed unscaled delta time: 0,02, Fixed delta time: 0,002, Fixed unscaled time: 6,017935, Fixed time: 1,204, TimeScale: 0,1
    [FIXED_UPDATE] Frame 24, Fixed unscaled delta time: 0,02, Fixed delta time: 0,002, Fixed unscaled time: 6,037935, Fixed time: 1,206, TimeScale: 0,1
    [FIXED_UPDATE] Frame 24, Fixed unscaled delta time: 0,02, Fixed delta time: 0,002, Fixed unscaled time: 6,057935, Fixed time: 1,208, TimeScale: 0,1
    [FIXED_UPDATE] Frame 24, Fixed unscaled delta time: 0,02, Fixed delta time: 0,002, Fixed unscaled time: 6,077935, Fixed time: 1,21, TimeScale: 0,1
    [FIXED_UPDATE] Frame 24, Fixed unscaled delta time: 0,02, Fixed delta time: 0,002, Fixed unscaled time: 6,097935, Fixed time: 1,212, TimeScale: 0,1
    [FIXED_UPDATE] Frame 24, Fixed unscaled delta time: 0,02, Fixed delta time: 0,002, Fixed unscaled time: 6,117935, Fixed time: 1,214, TimeScale: 0,1
    [FIXED_UPDATE] Frame 24, Fixed unscaled delta time: 0,02, Fixed delta time: 0,002, Fixed unscaled time: 6,137935, Fixed time: 1,216, TimeScale: 0,1
    [FIXED_UPDATE] Frame 24, Fixed unscaled delta time: 0,02, Fixed delta time: 0,002, Fixed unscaled time: 6,157935, Fixed time: 1,218, TimeScale: 0,1
    [UPDATE] Frame 24, Unscaled delta time: 0,0375348, Delta time: 0,00375348, Unscaled time: 6,177271, Time: 1,219934, TimeScale: 0,1


    We can see a negative fixed unscaled delta time, following by way too many FixedUpdate (since it tries to catch up with this negative time)

    I reprocude on many Unity version (2020.3.46, 2121.1.0, 2021.3.17...), and can't manage to find a workaround.
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    Perhaps this is because you're changing fixedDeltaTime in the coroutine, which runs during the update cycle, rather than the fixed update cycle?

    It's certainly weird behavior, though...
     
  3. Rudy_LPN

    Rudy_LPN

    Joined:
    Jan 3, 2023
    Posts:
    2
    I've tried doing it during Update, FixedUpdate, LateUpdate, after WaitForSeconds, WaitForEndOfFrame, WaitForFixedUpdate, with the same result. The documentation for timeScale does both in Update (https://docs.unity3d.com/ScriptReference/Time-timeScale.html).
    Not doing them at the same time is a bit tricky (if you update timeScale first, you'll have a long time before the first FixedUpdate, and if you do fixedDeltaTime first, you'll have many FixedUpdate before the first Update).
    However, if I don't seem to have negative unscaledFixedDeltaTime or too many FixedUpdate when I don't update fixedDeltaTime at the same time, I still have a strange unscaledFixedDeltaTime. I suppose it tries to scale differently part of the frame before and after the call to Time.timeScale, does it incorrectly and completly break if you change fixedDeltaTime before the next FixedUpdate.
     
    Last edited: Mar 30, 2023