Search Unity

Following Animator with animatePhysics is jerky

Discussion in 'Cinemachine' started by CDF, Feb 18, 2019.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Hey, so I need to follow and aim at an animator that has its updateMode set to "AnimatePhysics"
    No matter what update mode I choose for Cinemachine, the result is always jerky
    Rigidbody interpolation mode doesn't help either.
    I need to use animate physics so that when the animation stops, the rigidbody maintains velocities.

    Is it not currently possile to follow an animator with animatePhysics?
    I have a simple test project setup if you guys like.

    Using Cinemachine 2.2.8
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    This actually seems to be a problem with using Timeline.
    The animator is being driven by Timeline.
    If I manually update timeline in fixed update I get smooth camera action!
    However, now variable Time scale is an issue. I have a Time dilation clip on my timeline asset. And this isn't playing nicely with updating the timeline in fixed update :(

    Another strange quirk is I can update timeline manually in a late update with animate physics and get smooth camera with variable time scale. But then the velocities applied to the kinematic rigid body are all wrong, so at the end of the timeline, the rigidbody hasn't got accurate velocities and seems to skip.

    So basically I'm looking for a solution where:
    1. Animate a kinematic rigidbody using Timeline with animate physics = true
    2. Smooth follow and aim with damping
    3. Variable time scale on the timeline
    4. Return rigidbody to normal at the end of Timeline and inherit the velocities from the animation
     
  3. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    I suspect the manual updates of timeline are what are messing with the velocities, in particular doing it in late update maybe causing a double evaluation.

    If your timeline doesn't use audio, setting the time update mode to manual and calling playableDirector.Evaluate() in a fixedUpdate loop, taking into account Time.timeScale yourself to account for the time dilation track, may be the best option.
     
  4. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Thanks seant. I've modified the Time Dilation track to also scale the FixedDeltaTime value by Time scale:
    Code (CSharp):
    1. Time.fixedDeltaTime = 0.02f * Time.timeScale;
    This does work, but now seeing some more strange things happen on Cinemachine cameras with damping.

    In the videos below, both objects are animated along the timeline.
    The white object has a rigidbody and set to "animatePhysics"
    The blue object is animated as normal

    Here are some videos:

    1. Advance Timeline in FixedUpdate:
    Cinemachine damping is too strong, maybe doubled? Both objects are smooth


    2. Advance Timeline in FixedUpdate with Rigidbody Interpolation
    Blue object is jittery. damping is normal, white object is smooth


    3. Advance Timeline in FixedUpdate, CinemachineBrain set to ignore time scale
    Both objects smooth, damping is normal. But Cinemachine will now ignore time scale globally


    4. Advance Timeline in LateUpdate
    Both objects smooth, damping is normal. But white object has incorrect velocity at end


    5. Advance Timeline with GameTime (default option on Timeline)
    Both objects jerky, damping is incorrect. White object has correct velocity at end


    There really isn't an ideal case here.
    There must be something happening with the damping applied on Cinemachine, maybe Cinemachine is also scaling delta time by Time scale which is resulting in the double damping effect?
     
  5. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Yep, Cinemachine is also scaling FixedDeltaTime, resulting in double damping.

    Code (CSharp):
    1. private float GetEffectiveDeltaTime(bool fixedDelta)
    2. {
    3.     if (SoloCamera != null)
    4.         return Time.unscaledDeltaTime;
    5.  
    6.     if (!Application.isPlaying)
    7.     {
    8.         for (int i = mFrameStack.Count - 1; i > 0; --i)
    9.         {
    10.             var frame = mFrameStack[i];
    11.             if (frame.Active)
    12.                 return frame.TimeOverrideExpired ? -1 : frame.deltaTimeOverride;
    13.         }
    14.         return -1;
    15.     }
    16.     if (m_IgnoreTimeScale)
    17.         return fixedDelta ? Time.fixedDeltaTime : Time.unscaledDeltaTime;
    18.     return fixedDelta ? Time.fixedDeltaTime * Time.timeScale : Time.deltaTime; //Sad Face
    19. }
    I guess I'll have to manually set the ignore time scale whenever I have a timeline that needs to advance using fixed delta time :(
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Yeah... that's probably a mistake. We'll look into fixing it. Thanks for letting us know.
     
    AdamarHammerfell likes this.
  7. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Awesome thanks :)

    Removing Time.fixedDeltaTime * Time.timeScale from Cinemachine would solve my issue. But I guess could potentially have consequences with other peoples projects. Maybe having an option to "ignoreFixedTimeScale" so as to not break existing projects?
     
  8. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Hey @Gregoryl Just checking in on this. Noticed this is still an issue in CM 2.3.3, are there plans to fix soon, or is this on a backlog?

    If you need a bug report submitted to get it in the system, can do.
    Think I still have this project lying around somewhere

    Thanks
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    This is fixed on our trunk, should show up in 2.3.4
     
    CDF likes this.
  10. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Awesome, thanks
     
  11. Keepabee

    Keepabee

    Joined:
    Jul 12, 2012
    Posts:
    58
    The above doesn't really sound like a good common solution for Cinemachine+Timeline users, generally.

    So, if any Cinemachine-user uses "Animate Physics" and wants to do cutscenes etc. with Timelines, they'll have to find this forum post and roll their own Timeline updater script and accept the constraint that no cutscenes will ever be sequenced with sound in them, probably having to roll their own system for synchronizing Timeline animation with sound coming from non-Timeline source?

    Our project is running into this problem during a busy delivery week, and we're arguing solutions from making every cutscene NOT ever track animated actors however silly that will be artistically and trying to draft up whatever solutions to this issue.

    I think next we'll try to write our own manual update script for Cinemachine instead of Timeline, so we can keep using Wwise audio and Cinemachine camera integrations on Timeline with "Animate Physics" Animator-actors.