Search Unity

Hunting the Jerkiness (An issue of Fixed Stepping)

Discussion in 'Physics' started by LightStriker, May 11, 2018.

  1. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    We have a complex third person action-RPG where the player is animation-drive (Root Motion).

    Before you ask, we already tried having everything in FixedUpdate or Update.

    Here's the issue; we don't want to force VSync. The player should be allowed to unlock the FPS if he wishes. The game should play great at 30, 40, 60 or even 120 FPS.

    FixedUpdate Issue: If we put the Animator update on FixedUpdate, we get a number of frames not updated. If the Fixed Delta is 0.02 and we run at 60 FPS, we get 1 frame out of 6 without an animation update. The issue becomes more and more drastic as we increase in framerate. For quality, we should run on Update/LateUpdate.

    Update/LateUpdate Issue: While the animation is smoooooth, we get a odd jerkiness in the motion, as the physic stop the Animator motion on frame the physic isn't ticked. If we run at the same Fixed Delta with the same 60 FPS, the animation is updated on all frames, but 1 frame out of 6 the motion is not.

    Clearly we want both the animation AND the motion to be smooth. The higher the framerate, the more pronounce the issue appears. Higher framerate should be smoother, not jerkier!
     
    Last edited: May 11, 2018
  2. ThermodynamicsMakesMeHot

    ThermodynamicsMakesMeHot

    Joined:
    Feb 14, 2015
    Posts:
    224
    Have you tried playing with the timeStep? Certainly sounds like it could help here.
     
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Sure... but how can you guaranty you will never skip a frame at higher framerate? Keep changing the timeStep higher or lower to the average of delta time?
     
  4. ThermodynamicsMakesMeHot

    ThermodynamicsMakesMeHot

    Joined:
    Feb 14, 2015
    Posts:
    224
    Well what we did was create a TimeManager that adjusted the timeStep depending on the situation, like you mentioned you can't just leave set at something because it will have issues.

    There is not really an exact science to find the best results, we found the best settings by trial and error. ScriptableObject helps here so you can adjust in realtime and save those changes.
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    I'm a bit surprised, because this plagued us for years!

    Moments after posting this thread, I decided to try turning off autoSimulation and to call Physics.Simulate() manually in a manager Update loop.

    Freaking.

    Flawless.

    Even the camera jerkiness in high speed elevators is gone.
     
  6. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Just calling Physics.Simulate() within Update()? Can you elaborate on this?
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    https://docs.unity3d.com/ScriptReference/Physics-autoSimulation.html

    Turning that setting off prevent the physics from advancing the simulation. FixedUpdate is still called, OnTrigger, OnCollision, etc. are still called, but no object are moved around.

    https://docs.unity3d.com/ScriptReference/Physics.Simulate.html

    When Simulate() allows you to step the simulation by the time you supply it. So by running a script on Update, I step the physic simulation every visual frame. That script has a Script Execution Order to make it tick first.

    Possible downside:
    - Not longer possible to be deterministic.
    - OnTrigger/OnCollision/etc are called one frame late.
    - A low framerate may have collision issues, as the physic engine is used to run it's simulation at 50 FPS.
     
  8. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Thanks, that was what I wanted to know. I'm aware of the Physics.autoSimulation setting, but just wondered if it could be used this way for running physics on each visual update.

    Physics hasn't ever been deterministic anyways. Low framerate situations might be worked around by calling Physics.Simulate several times per frame for ensuring some minimum physics update rate.
     
  9. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Quite true. Just need to be careful as to not make multiple Simulate call ending up dragging the framerate even lower...

    But yeah, it's soooo smoooooth! You wouldn't believe it's not butter!
     
    hippocoder likes this.