Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Exceptions when changing Time.timeScale

Discussion in 'Cinemachine' started by bromanz, Jan 19, 2018.

  1. bromanz


    Jan 30, 2017
    In our application we have the option to pause every physics behaviour by setting Time.timeScale = 0; and unpausing it by restoring it to Time.timeScale = 1;. During the pause, CM doesn't work, which is expected. However, immediately after restoring Time.timeScale to 1, numerous exceptions are thrown:

    Code (CSharp):
    1. Assertion failed: Assertion failed on expression: 'CompareApproximately(det, 1.0F, .005f)'
    2. Cinemachine.Utility.PositionPredictor:PredictPosition(Single) (at Assets/Cinemachine/Base/Runtime/Core/Predictor.cs:64)
    3. Cinemachine.CinemachineComposer:GetLookAtPointAndSetTrackedPoint(Vector3) (at Assets/Cinemachine/Base/Runtime/Components/CinemachineComposer.cs:132)
    4. Cinemachine.CinemachineComposer:PrePipelineMutateCameraState(CameraState&) (at Assets/Cinemachine/Base/Runtime/Components/CinemachineComposer.cs:151)
    5. Cinemachine.CinemachineVirtualCamera:CalculateNewState(Vector3, Single) (at Assets/Cinemachine/Base/Runtime/Behaviours/CinemachineVirtualCamera.cs:420)
    6. Cinemachine.CinemachineVirtualCamera:UpdateCameraState(Vector3, Single) (at Assets/Cinemachine/Base/Runtime/Behaviours/CinemachineVirtualCamera.cs:128)
    7. Cinemachine.CinemachineCore:UpdateVirtualCamera(ICinemachineCamera, Vector3, Single) (at Assets/Cinemachine/Base/Runtime/Core/CinemachineCore.cs:237)
    8. Cinemachine.CinemachineCore:UpdateAllActiveVirtualCameras(Vector3, Single) (at Assets/Cinemachine/Base/Runtime/Core/CinemachineCore.cs:172)
    9. Cinemachine.CinemachineBrain:UpdateVirtualCameras(UpdateFilter, Single) (at Assets/Cinemachine/Base/Runtime/Behaviours/CinemachineBrain.cs:445)
    10. Cinemachine.CinemachineBrain:LateUpdate() (at Assets/Cinemachine/Base/Runtime/Behaviours/CinemachineBrain.cs:399)
    13. Assertion failed: Assertion failed on expression: 'fRoot >= Vector3f::epsilon'
    14. Cinemachine.Utility.PositionPredictor:PredictPosition(Single) (at Assets/Cinemachine/Base/Runtime/Core/Predictor.cs:64)
    15. Cinemachine.CinemachineComposer:GetLookAtPointAndSetTrackedPoint(Vector3) (at Assets/Cinemachine/Base/Runtime/Components/CinemachineComposer.cs:132)
    16. Cinemachine.CinemachineComposer:PrePipelineMutateCameraState(CameraState&) (at Assets/Cinemachine/Base/Runtime/Components/CinemachineComposer.cs:151)
    17. Cinemachine.CinemachineVirtualCamera:CalculateNewState(Vector3, Single) (at Assets/Cinemachine/Base/Runtime/Behaviours/CinemachineVirtualCamera.cs:420)
    18. Cinemachine.CinemachineVirtualCamera:UpdateCameraState(Vector3, Single) (at Assets/Cinemachine/Base/Runtime/Behaviours/CinemachineVirtualCamera.cs:128)
    19. Cinemachine.CinemachineCore:UpdateVirtualCamera(ICinemachineCamera, Vector3, Single) (at Assets/Cinemachine/Base/Runtime/Core/CinemachineCore.cs:237)
    20. Cinemachine.CinemachineCore:UpdateAllActiveVirtualCameras(Vector3, Single) (at Assets/Cinemachine/Base/Runtime/Core/CinemachineCore.cs:172)
    21. Cinemachine.CinemachineBrain:UpdateVirtualCameras(UpdateFilter, Single) (at Assets/Cinemachine/Base/Runtime/Behaviours/CinemachineBrain.cs:445)
    22. Cinemachine.CinemachineBrain:LateUpdate() (at Assets/Cinemachine/Base/Runtime/Behaviours/CinemachineBrain.cs:399)
    24. Look rotation viewing vector is zero
    If you're using a following CM virtual camera utilizing a Transposer and Composer, the camera ends up jittering for 1-2 secs after unpausing due to the exceptions.

    My current workaround solution includes the following changes in Predictor.cs:58

    Code (CSharp):
    1. Vector3 vel = (m_Velocity.IsEmpty() || !IsValid(m_Velocity.Value())) ? : m_Velocity.Value();
    2. Vector3 accel = (m_Accel.IsEmpty() || !IsValid(m_Accel.Value())) ? : m_Accel.Value();
    The velocity and acceleration are now also set to if m_Velocity or m_Accel values are not valid, i.e. their vectors include one float which is infinity or NaN. This is the case after restoring Time.timeScale = 1 (and this could maybe happen in other edge cases too I guess?). Below I added the function:

    Code (CSharp):
    1. private bool IsValid(Vector3 v)
    2. {
    3.   return !float.IsNaN(v.x) && !float.IsNaN(v.y) && !float.IsNaN(v.z) && !float.IsInfinity(v.x) && !float.IsInfinity(v.y) && !float.IsInfinity(v.z);
    4. }
    Hope that this helps someone else out and a similar fix will playback into the next CM version :)
  2. Gregoryl


    Unity Technologies

    Dec 22, 2016
    Hey @bromanz Thanks for posting this!

    While your fix will make the exceptions go away, it's probably better to correct the error at the source. This happens in PositionPredictor.AddPosition() and PositionPredictor.PredictPosition(), when these methods divide by deltaTime which, if zero, will generate NaNs. Probably it's best if these methods simply check for deltaTime==0 before dividing.

    We will include a fix in the next release of CM.

    For now, you can patch Predictor.cs with these replacement methods:
    Code (CSharp):
    1.         public void AddPosition(Vector3 pos)
    2.         {
    3.             if (IsEmpty)
    4.                 m_Velocity.AddValue(;
    5.             else if (Time.deltaTime > Vector3.kEpsilon)
    6.             {
    7.                 Vector3 vel = m_Velocity.Value();
    8.                 Vector3 vel2 = (pos - m_Position) / Time.deltaTime;
    9.                 m_Velocity.AddValue(vel2);
    10.                 m_Accel.AddValue(vel2 - vel);
    11.             }
    12.             m_Position = pos;
    13.         }
    15.         public Vector3 PredictPosition(float lookaheadTime)
    16.         {
    17.             Vector3 pos = m_Position;
    18.             if (Time.deltaTime > Vector3.kEpsilon)
    19.             {
    20.                 int numSteps = Mathf.Min(Mathf.RoundToInt(lookaheadTime / Time.deltaTime), 6);
    21.                 float dt = lookaheadTime / numSteps;
    22.                 Vector3 vel = m_Velocity.IsEmpty() ? : m_Velocity.Value();
    23.                 Vector3 accel = m_Accel.IsEmpty() ? : m_Accel.Value();
    24.                 for (int i = 0; i < numSteps; ++i)
    25.                 {
    26.                     pos += vel * dt;
    27.                     Vector3 vel2 = vel + (accel * dt);
    28.                     accel = Quaternion.FromToRotation(vel, vel2) * accel;
    29.                     vel = vel2;
    30.                 }
    31.             }
    32.             return pos;
    33.         }
    Thanks again!
    Last edited: Jan 19, 2018
    Abladur and bromanz like this.
  3. paulindrome


    Mar 10, 2018
    Sorry for digging up an old thread but this problem seems to still be around.
    I've tried working around this with various bruteforce tricks but nothing saves me from having to use a timescale of 0.005f or lower instead of actually pausing the game with a timescale of 0 flat.

    (Btw. I love using cinemachine. No idea how I ever got by without it.)
  4. Intense_Gamer94


    Jun 20, 2013
    Does checking "Ignore Time Scale" in the Cinemachine Brain help?

    I know, Cinemachine is amazing!
  5. Gregoryl


    Unity Technologies

    Dec 22, 2016
    @paulindrome What version of CM are you using?
    Did you apply the patch I posted above? That should make the exceptions go away.
    No need to set IgnoreTimeScale in the brain.
  6. tcz8


    Aug 20, 2015
    Same problem here on cinemachine 2.1.10 and unity 2017.4.3f1

    Trying to patch it now... brb

    Result: The patch works perfectly!

    @Gregoryl thank you.
    Last edited: Jul 5, 2018