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. Dismiss Notice

Camera jitter when using interpolation for player

Discussion in 'Cinemachine' started by centaurianmudpig, Jul 10, 2022.

  1. centaurianmudpig

    centaurianmudpig

    Joined:
    Dec 16, 2011
    Posts:
    91
    Hey, I posted this originally in Physics but hoping there may be some hope in posting here, especially with Gregoryl being hot on answering threads in here.


    I have a jitter problem with a large-sized model moving at fast speeds when interpolation is enabled for the hero character. It is important to have interpolation on to make the scene appear smoother, it's just the hero character jitters.

    I have a cinemachine camera set to follow, but with some testing, this appears more relevant to Unity/Physics than cinemachine itself.

    I have included an example project showing a follow object's jitter. It's a scaled cube but represents the actual size of the model with its colliders that I am using in my project that is scaled to 1,1,1. and jitters just as much.

    In the example project, I've made it as simple as I could, an initial force is applied to the ship and that's it. The ship then continues under its own momentum without any drag.

    Needs:
    • Camera to track the ship (player)
    • Interpolation enabled for the ship (player) for smoother camera movement of the game world.
    • Dampening on the camera is needed on the cinemachine camera for the ship to feel like it has weight in movement and rotation.
    • Appropriate sized models and mass to maintain the weight of physics interactions of warship-sized ships
    Problem:
    Enabling interpolation causes the ship (player) to jitter while the camera moving around the game world appears smooth.
     

    Attached Files:

    gaborkb likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Thanks for the project upload. I appreciate it when people make the effort to isolate the issue.

    When you enable interpolation, the effect is that physics will, at Update time, patch the transform of the object to put it in the right place for the current deltaTime, so that the rendered frame shows it in the most accurate place. This effectively "converts" the FixedUpdate animation to Update animation. Therefore, the CM cameras should treat the targets as being animated in Update.

    In your project, the CM Brain is set to FixedUpdate, which means that the cameras are tracking the FixedUpdate target position when for smoothness they should be tracking the Update position, because that's where the target will be rendered.

    The solution is to switch the CMBrain to work in LateUpdate, not FixedUpdate:

    upload_2022-7-10_12-28-0.png
     
    dennissally1 and gaborkb like this.
  3. centaurianmudpig

    centaurianmudpig

    Joined:
    Dec 16, 2011
    Posts:
    91
    Thank you very much @Gregoryl for the quick reply and the explanation of interpolation. It's smoother but there is jitter if I increase the speed of the ship and reduce the damping on the z-axis to 0.1. The speed of the ships is much greater than the average speed in my project but shows the snapping back effect quite well and more frequently.

    I am aware there would be issues further from the world center you go, I have a floating origin routine to avoid this and the same snapping back effect happens. The attached zip file has the floating origin for comparison.

    MoveTheWorld script is the floating origin routine. You can toggle UseFloatingOrigin via the script on the gameobject.
     

    Attached Files:

  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    I think the remaining jitter issues are due to the uneven frame rate when playing in the editor. It's quite smooth when I make a build of the project. Because damping is a simulation and because a follow camera is involved, it is especially sensitive to framerate fluctuations. You are pushing the limits with this setup: high speed, camera following closely but with damping. Any framerate glitch is going to be highly visible.
     
  5. centaurianmudpig

    centaurianmudpig

    Joined:
    Dec 16, 2011
    Posts:
    91
    Thanks for your help. I can see the jitter is worse with an uneven frame rate especially when I uncap that in the build.
    I'll keep this in mind.
     
  6. centaurianmudpig

    centaurianmudpig

    Joined:
    Dec 16, 2011
    Posts:
    91
    As a note to anyone who comes across this post and finds interpolation does not work for them:

    When I enabled interpolation I found out of sync problems with raycasts and audio (basically the jitter is now happening out of sight in the Physics for a lack of a better explanation).

    Interpolation is not going to work for my project. However, jittering without interpolation is down to lower frame rates using the default values. I'm going the route of syncing the physics update to the frame rate to try and get smoother performance.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    I would recommend investigating this a little farther. Interpolation just patches the transform for the purposes of rendering (and since cameras care about the rendered image, they should track that). Any logic that depends on FixedUpdate positions should continue to work, and should remain in FixedUpdate.
     
  8. centaurianmudpig

    centaurianmudpig

    Joined:
    Dec 16, 2011
    Posts:
    91
    I truly appreciate your recommendation. I'm not sure how I would go about investigating further. I've tried all I could, based on what I know and what I could learn from other such posts, though there seems to be plenty of misunderstandings on the subject in general. I may just need to spend some time away from the problem before looking at it again.

    Right now, setting physics to the frame rate works great without interpolation, and from what I read is what AAA games do like GTAV). My game object movement is relatively simple, all done in Fixed Update applying Impulse forces to a rigidbody. Interpolation just seems to break more things while solving the camera jitter.
     
  9. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    1,655
    @Gregoryl
    I can confirm that Cinemachine does not work at the moment with Rigidbodies that have interpolation turned on, no matter the brain update setting. A few customers have reported this too. The only way to make it follow a vehicle smoothly is to turn the interpolation off and use the FixedUpdate as the brain update - which is far from ideal.

    To reproduce:
    - set the FPS to 60 or lower.
    - set the Project Settings > Time > Fixed Delta Time to 0.01 (100Hz physics update).
    - Make the Cinemachine camera follow a vehicle that is moving above 50 km/h and has Interpolate on the Rigidbody.

    If still having issues reproducing, reduce any smoothing to a near 0 value.
     
    Unifikation likes this.
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    What version of Unity?
    What version of CM?

    When I try it with 2021.3 and CM 2.9.5, it is indeed a bit choppy under these conditions in the editor. However, it's perfectly smooth in a build.
     
  11. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    1,655
    It is Unity 2020.3.40 and latest Cinemachine for that version of Unity. At higher speeds, e.g. 120 km/h or even while the camera is rotating there are what looks like spikes in jitter. Could be due to a coroutine updating in the background, but I also have my own camera that is set up with the similar follow behavior and is updating in LateUpdate() and is perfectly smooth.

    I actually did not notice any of this on my desktop PC which runs the project at 300+ FPS. But when I tried on a Ryzen 6800H laptop with Radeon 680M iGPU and ~50 FPS, it started getting obvious. I have also had multiple customers report the issue. Everything seems fine until about 10m/s, choppy after that. And while the vehicle is sitting still the camera rotation almost seems like it is running at 10 FPS. Can do a video if needed as the effect is quite pronounced both in editor and in build.
     
  12. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    1,655
    Here is the video, first 15 seconds is Cinemachine, after that is my demo camera. Notice how when going straight Cinemachine is failing to keep the distance, instead it seems almost as if it extrapolating and the overshooting. At times it looks like it is failing to focus. I wonder if this is the look ahead issue as I have it set to 0.4s.

     
  13. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    1,655
    @Gregoryl
    Upon further investigation it seems that the issue is related to the CinemachineVirtualCamera damping:

    upload_2023-5-7_19-41-13.png

    X, Y and Z set to 0 fix the issue.
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Camera judder that disappears when you set damping to zero can be symptomatic of a target that is not being moved evenly. In my test with Rigidbody motion, I found that the motion is smooth (in the build) when both interpolation and damping are enabled.

    One question I have: how are you moving the Rigidbody? What APIs are you using? I ask because it's easy to use the wrong RB API and break interpolation. Interpolation not working correctly would produce the symptoms you describe.

    It would be helpful if you could share with me a simple project (e.g. just a cube for a target) that reproduces this issue, with the same camera and motion setup. Then we could discuss it more concretely.
     
    Last edited: May 8, 2023
  15. slumberface

    slumberface

    Joined:
    May 22, 2016
    Posts:
    15
    @Gregoryl

    Can you upload your project where using Cinemachine + Rigidbody + Interpolation + Damping produces smooth rotations in camera?
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Look at the "Free Look character" sample scene that ships with CM. You can turn on interpolation in the 3rdPersonController object.
     
  17. slumberface

    slumberface

    Joined:
    May 22, 2016
    Posts:
    15
    @Gregoryl
    This example scene "Free Look Character" can be used to demonstrate the bug: Instead of the free look camera, setup a virtual camera like this:
    upload_2023-7-20_16-12-53.png

    With these settings on the character movement:
    upload_2023-7-20_16-14-11.png

    The only way I can get smooth turning with the camera following the character's forward is to have no interpolation on the rigidbody, and have the brain in fixed update mode.

    The movement script on this character is also updating the rigidbody's rotation using "transform.rotation" when the docs suggest using rigidbody.MoveRotation() to respect interpolation settings. That being said, in my tests, it does not make a difference here.

    In fact, is there a simple way for us to verify that rotational Rigidbody interpolation is actually working as intended on rigidbodies in the first place?
     
    Last edited: Jul 20, 2023
    KellyCode likes this.
  18. slumberface

    slumberface

    Joined:
    May 22, 2016
    Posts:
    15
    @Gregoryl have you had a chance to look into this?

    I think I've seen you recommend in other places that ideally you just shouldn't be tracking fixed update transforms with cinemachine in the first place, and should instead have tracking objects that manually interpolate their position/rotation by some smoothing function to follow the rigidbody, and have the virtual camera track the follow object rather than the rigidbody.

    At the end of the day, is this simply the advisable approach?
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Sorry for the slow response.

    Yes, the code in the FreeLookCharacter sample is bad, it should not be touching the transform. We've got all-new samples in CM3 that are better behaved.

    It's possible for Cinemachine to track objects on FixedUpdate, but it's not optimal. Enabling Interpolation on the Rigidbody is a much better solution, if you can manage it. Getting rotational interpolation to work is a little tricky, and the physics docs IMO are a little misleading.

    Attached is a small sample scene that shows how to rotate a RigidBody in such a way as to keep interpolation working (the secret is to use rb.angularVelocity). Also, commented out, is code showing how NOT to do it. See CubeMove.cs.
     

    Attached Files: