Search Unity

Rigidbody jitter / lag when applying damping on Transposer

Discussion in 'Cinemachine' started by andracer108, Jun 11, 2018.

  1. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Hello everyone! I do not suppose anyone can help me with this issue I'm having?

    I have a system where a Rigidbody that floats can be controlled via the keyboard (for example, accelerating it and slowing it down). I use multiple scripts to define characteristics such as a simple aerodynamics script, a floating script, movement script, etc. The camera I set up uses Cinemachine and follows and looks at the transform of the floating Rigidbody. However, when I do some damping on the Transposer, at high speeds, the object seems to start jittering. It is especially noticeable while it is not detecting the ground and flying high.

    I have tried multiple solutions but none of these seem to work. These were: making sure all Rigidbody modifications were done in FixedUpdate, using different update methods in the CinemachineBrain, messing around with other settings in the Transposer and Composer, creating an empty GameObject that would follow the Rigidbody and setting Cinemachine to follow the empty GameObject instead, simplifying the system as much as possible by making it use one script and the basic Rigidbody drag instead of the aerodynamics script, and I even re-created my project as simple as possible to see if the problem was still there, but it was!

    I might add that the Rigidbody I currently use has "Interpolate" on and I read that there was once an issue with Rigidbodies using "Interpolate" in Cinemachine. When I turned "Interpolate" off, the Rigidbody only started to jitter more vigorously. Where should I start trying to fix this issue?

    Thank you!
    Regards,
    Andracer108

    P.S. Attached, there are also my settings for Cinemachine. The fields surrounded with the red square brackets are the ones which make the jitter visible if anything larger than 0 is inputted. If any more info is needed, such as code, feel free to ask. :)

    cinemachineissue2.PNG cinemachineissue1.PNG
     
    Last edited: Jun 12, 2018
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Yes, damping in CM cameras can cause jittering if the animation is unsteady. However, you have already outlined all the recommendations I would have made to fix it. At this point, I'd really like to see an example project so I can dig a little deeper into this. Could you package a simple example for me that shows the problem?
    Thanks
     
  3. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Thanks for the reply! I have prepared a simplified scene with a simplified system (I had to modify some of the code, but the problem does still show itself a bit). I will have a total of 6 scripts that I believe are needed for testing. I also made a basic explanation on each of them. Keep in mind, that in this simple scene (since I used just some Unity primitive objects and not a fully-fledged model), the jitter isn't that apparent. But, if you press the accelerate key (W) for a while, you will notice small jittering. This "lag" was more prominently shown when using a 3D model. If needed, I can try to record a small video in the actual project. Also, apologies in advance if it is something from my code and not from Cinemachine itself.

    Attached please find the packaged simple scene. If there is something wrong with the package, please do inform me as this is actually the first time I had to export a package and send it to someone for a debug or code review. Thanks again!
     

    Attached Files:

    Last edited: Jun 11, 2018
  4. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Sorry! My mistake! I meant Transposer and NOT Composer. The problems only appear if I add damping in the X, Y, Z axes of the Transposer. By the way, any updates? Not trying to rush you, just curious if you have started working on it and if the problem is actually from my side. :)
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    If I turn off interpolation on the vehicle the jitter disappears.

    upload_2018-6-12_12-51-16.png
     
  6. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Well, that seems to be true and thank you for offering a solution. But I actually need "Interpolate" as the vehicle in my actual project just jitters all over the place without it. I'm trying to find a solution to this as I'm typing this but I still need "Interpolate" for a slow down effect and it is much more efficient to use "Interpolate" than increasing the FixedTimestep. Increasing the FixedTimestep would also make the physics update more than they are supposed to even during the slow down effect. Are you sure that there is no other solution to my problem than this?

    UPDATE: I have discovered that what makes the jitter visible is actually the motion blur post-processing effects that I have applied in my actual project. I did the same in the simple project I sent you and it actually does jitter. It doesn't jitter constantly, but as you turn or accelerate, the jitter is visible. Again, I would like your help as just setting "Interpolate" off does not solve the problem. If you want, I can send you what jitter is going on in my actual project and in the simple scene as a video.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Damping-related judder in the CM cameras is the result of animation that is irregular, meaning that some frames have spikes in their movement. These spikes cause spikes in the damping, which is visible as judder.

    FixedUpdate guarantees a fixed timestep, so things animated by the physics system (which is procedural and smooth) won't spike, and if the camera also updates on those fixed timesteps the damping will be happy. What Interpolate does is to interpolate between physics frames to approximate the object's transform at the irregular Update() time which in general occurs between fixed frames. If these Update times are not occurring steadily, then the time intervals will be irregular (deltaTime is only a guess, based on the last frame), and the animation will look irregular to the damping system, and you will see spikes. If you really want to keep Interpolate on, you will have to somehow ensure that your framerate is very steady. Either that or turn off damping.

    For the PostProcessing thing, can you add the post stuff to the sample scene and send it to me again?
     
  8. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Could you explain from where the irregular animation can be coming from or ideas of what it could be? The only "animation" that I had in my actual project is by rotating the transform to make the vehicle look like it's banking (I disabled this and the problem was still there).

    Any ideas on how I can ensure a steady framerate? I will probably disable damping for now but I will probably want it in the future. So I would appreciate it if you were to offer me some ideas. Or is it as simple as decreasing the amount of content?

    And yes, I would be glad to share the package again as an attachment. I have also added the post-processing stuff and added the 3D model I am using in my actual project which you can use to test as well.
     

    Attached Files:

  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    On a PC or mac, the irregular framerate rate is pretty much impossible to control, because the OS interrupts the game and does its stuff whenever it wants. On a game console things are more stable. Bottom line is that you have to design your game around the inevitability of an irregular framerate.

    So if your vcams are damping and the target is animated in FixedUpdate, that pretty much means turning off interpolation on those targets.

    Looking at your PostProcessing profile, it seems that the artifacts are coming from the Motion Blur module. Anti-aliasing is fine. I suggest you turn off motion blur, or ask on the PostProcessing forum: https://forum.unity.com/forums/image-effects.96/
     
  10. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Despite following all of your advice there is still some jitter visible in my actual project. I have tried everything to fix this including applying your solutions (and if I were to be honest, I would have liked more work to be done as I actually need motion blur and interpolation and I will only look at these as possible temporary workarounds). I also loaded up an official and similar Unity project from the 2017 version which uses Cinemachine. The setup in that project is similar to mine (for example, having the vehicle as an empty object with its children defining the model, collision, sensors, etc) and all the essential settings (Time settings, Quality settings, etc.) are the same and even the Cinemachine setup is the same and it showcases no problems whatsoever. What more steps can I try to fix this problem? Nothing else actually modifies the object.
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Well, if it works with the sample scenes but not with your project, the thing to do is to isolate the key difference, perhaps by process of elimination. Start removing stuff until the problem goes away, then figure out which of the removed things is (are) causing the problem. Then we'll see what we can do about it.

    Can I ask why say that you need interpolation?
     
    Last edited: Aug 13, 2020
  12. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    The thing is, I already did that before. If you look at my package, you would see I have a script called BasicDrive and this pretty much eliminates the use of all the other scripts. The only things that would be needed for that script to work is the Rigidbody component with the drag values already set. I had tried that in my actual project but the problem was still there. I even removed all the unnecessary scripts (such as one controlling a particle system for thrusters) to test it out but, again, the problem was still there. I also removed the unnecessary child objects as well, I think at one point.

    Regarding interpolation, if you scroll up to one of my previous replies, I said that I needed interpolation for an effect that I need in my game. The effect is slow motion. To efficiently do slow motion (or at least from what I read), you need to use Rigidbody interpolation as slowing down the time scale without interpolation will cause the physics to seem like they have "less resolution" and you could see the physics system work frame by frame, if the time scale is slowed down enough. Increasing the Fixed Timestep helped to solve the problem but that is extremely inefficient as it would require the CPU to work faster to compute the forces acting on the object, apart from the fact that the results would be inaccurate as even though the time scale is slowed down, the physics will work at an increased rate.
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @andracer108 I had a closer look at this and discovered a couple of things.

    What Interpolation does is to compensate for the fact that a render frame will in general fall between physics frames. It does this by interpolating the state of the RigidBodies in the 2 neighbouring physics frames, and applying the delta to the transforms prior to Update(). However, this interpolation is not pushed back to the physics simulation. The result is that the transforms of the RigidBodies are changed in both FixedUpdate (by the simulation), and in Update (by the interpolation).

    So, in principle, if you set your Brain to LateUpdate, it should pick up only the interpolated values, not the physics frame values, and so the vcams should be reasonably smooth.

    In theory.

    In practice, it turns out that there was a bug in Interpolation (!) causing the interpolation to be unsmooth on some frames. Hence the jitter. The good news is that this bug has been fixed in Unity 2018.2. I tried it with your test scene, with interpolation, and it's nice.

    Also, there is a new version of Cinemachine in preview (v2.2.2) that has a change that allows SmartUpdate to detect interpolation and update the relevant vcams on LateUpdate. So now we can say that the vcams support interpolation.

    Note: Interpolation is fragile, and if you directly modify the transform of the rigid body that you're interpolating, that will invalidate the following precalculated physics frame, the result being that interpolation will be unavailable for that frame, and maybe you'll see a glitch. This is a gotcha that people do trip over, so be careful when using interpolation.
     
    Last edited: Jun 20, 2018
    andracer108 likes this.
  14. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    @Gregoryl Thanks for the info! In fact, I already had updated Cinemachine to version 2.2.0 from the Package Manager. For some reason though, it doesn't seem to be able to find a 2.2.2 version (do you know where I can get that by any chance?). Despite that, I decided to test your theory anyway and, when I enabled LateUpdate in the Brain, the camera updated normally without jitter (with Interpolation on)!

    So, the problem could have potentially been Unity's actual Interpolation? I'll check if there is jitter when I enable damping on 2018.2. I'll reply with a status update so I can inform you if the problem is fixed or not.

    Also, thank you for the note regarding Transform and Interpolation. I'll keep it in mind when developing. :)
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    2.2.2 is in preview - you have to set your project manifest to point to Unity's staging repo. I wouldn't hurry to do that if I were you. Wait for the stable release :)

    The relevant change for you in that version is that you'd be able to set the brain to SmartUpdate instead of LateUpdate, and it would recognize the targets with interpolation and handle them correctly.
     
  16. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Are you saying that I should wait until a stable 2.2.2 release to see if my problem is fixed? You see, I upgraded to the 2018.2 beta and tested it out, but the problem is still there in my actual project... as described before, I added damping to the transposer (to be exact, I added 0.05 damping on the Z axis) with an interpolated Rigidbody on SmartUpdate and when the object reaches high speeds, it starts to jitter a bit. I tested it out on LateUpdate as well, but the jitter was still there with the damping on.
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Framerate spikes will cause camera glitches if you have vcam damping when target speeds are high. Removing the damping will make the glitches go away.

    So the real question is: why are there framerate spikes? There are a number of reasons for these to happen, not least among them being the editor repainting its windows or doing other work while the game is running. To minimize these effects, do this kind of testing with the game view maximized and no other Unity widows visible on screen. Even better, eliminate the editor altogether by doing these tests in a build.

    We've recently identified some issues in Unity that are causing the spikes in the editor and are working to address them. 2018.3 already has some of these improvements.

    To your specific question: Yes wait for a stable CM before upgrading. The current preview version is 2.2.4, and we'll keep updating it as issues are identified in it by our QA. After upgrading, you should be able to use SmartUpdate with your interpolated targets. You may still see the occasional camera glitch at high speeds if you have damping, but these should be minimal when you test with a build. And, with Unity 2018.3, it will be better still.
     
  18. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Hi again! Sorry for necro-ing the thread but, I have updated Cinemachine to 2.2.7 (I'm not sure if there were other public versions before this because the Package Manager didn't show me there were any updates until today) and the problem is still there. When I apply damping on my Transposer, the vehicle starts jittering. After I updated, I even closed and opened Unity again but the problem is still there. I'm not sure what to do next now...
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @andracer108 Sorry to hear that the problem is still there. Let's see if we can do something about it. Let me have another look at your project and get back to you.
     
  20. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    I've looked at your project with CM 2.2.7 and Unity 2018.2, and here is what I'm getting, using SmartUpdate:
    1. With damping, and without interpolation on vehicle: perfectly smooth
    2. Without damping, and with interpolation on vehicle: perfectly smooth
    3. With damping, and with interpolation on vehicle: mostly smooth, occasional glitch.
    Is this consistent with what you're seeing?
     
  21. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    In my whole project, when interpolation on my vehicle is off, the vehicle starts jittering in a different way. The jittering is more aggressive and looks like heavy lag. To fix this, I turned on interpolation. When I have damping on and interpolation on, the vehicle doesn't do an occasional glitch, it just jitters constantly in a more subtle way but you can still see it when you compare it without the damping.

    If you want, I can try to upload the actual project that I'm working on currently with some trimmed out parts to make the upload faster.
     
  22. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    So that's not consistent at all with what I'm seeing. Clearly there is some missing info here. Maybe it's best if you prepare a new project for me to look at.
     
    andracer108 likes this.
  23. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Yes, as we speak I am trying to prepare a project with my current hover car system and testing other things out too. I will upload it here sometime this week as I'm currently busy with the project itself and with other things.
     
  24. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Sorry it's taking a while. Again, I'm not having much chance... I'll try to create and upload a new test project this weekend. However, I do have a theory. I think it may be the 3D model itself that is causing the issue. I will test this out soon.
     
  25. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Hi. Just wanted to give you an update. I have re-created my project from scratch in a beta build of Unity. (The one with the Improved Prefabs) and I can confirm that the problem is not from the model or the Unity project itself. It has to be either from my own code or Cinemachine.

    Also, I'm sorry for the delay. I will create for you a project with a proper Unity build for you to look at. I just wanted to do this little experiment first. I'll try to create it this week. If I cannot, I will post an update here again telling you if I found something new.
     
  26. TreborJones

    TreborJones

    Joined:
    Feb 5, 2015
    Posts:
    18
    Hi guys,

    I've been following this thread as I've had the same problems. In my project I have a similar setup - an interpolated rigidbody and Cinemachine camera that I'd like to put some damping values on.

    Appreciate that it's not a simple fix and that frame rate issues throw a spanner in the works, but thought I'd share a very simple sample project I built to demonstrate the issue. Just a falling rigidbody sphere with interpolation on, and a camera with some damping on the Y.

    My first time uploading a project, so not sure if this is the best way but here goes! :) Scream if it doesn't work. Hope it's helpful, and fully understand that it may not be quickly resolvable.

    Cheers,

    Bob.
     

    Attached Files:

    Last edited: Aug 30, 2018
    Gregoryl and andracer108 like this.
  27. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Hey Bob! Thanks for uploading the project. You showed me up! Just kidding. :p Honestly, I'm kind of relieved to see that someone else is also experiencing these issues. I will download your project and see if I have the same issues on it too. Meanwhile, my test project is almost done. I will also upload it when I have finished it as well.
     
    TreborJones likes this.
  28. Jde

    Jde

    Joined:
    Oct 2, 2011
    Posts:
    139
    If I understand correctly, it's not currently possible to get smooth damped motion when tracking an interpolated rigidbody with a varying framerate. Is that correct @Gregoryl?

    We experienced this issue (or a similar one) with one of our previous games, and the solution we used was to always do camera damping calculations (actually it was more of a spring calculation) in FixedUpdate, and then manually add the rigidbody interpolation to the camera in LateUpdate. The rigidbody interpolation is just the difference between the current LateUpdate and the previous FixedUpdate, so it looked something like this in the LateUpdate part...

    camPosition = dampedCamPositionFromLastFixedUpdate + (rigidbodyPositionLateUpdate - rigidbodyPositionFixedUpdate);


    It's possible I've overlooked something here, but I think that would smooth out the damped motion because the damping calculations would always be done at a consistent framerate?
     
  29. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Alright, so I have created my project. It's a very simple project with only a hovering object on a floor. It also has only 1 script in it that makes the hovering object move (by pressing W key) and "hover". Once you press and hold W, you will quickly see the damping problem and the object starts jittering.
     

    Attached Files:

  30. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @andracer108 Thanks for uploading the project. I was able to reproduce the jitter and can confirm that it's CM's damping algorithm being intolerant of the framerate variation at high speeds. The good news is that CM 2.2.8 (currently in preview) has a new experimental damping algorithm designed to help in these situations. It's opt-in (you have to define something in Player Settings), and when I tested your scene with it it was much better.

    @TreborJones Thanks Bob for uploading your scene too. That one showed a different problem: Composer Lookahead doesn't like irregular framerates either. I don't have a fix for that yet, other than to turn LookaheadTime to zero, but I appreciate you drawing my attention to this.

    @Jde thanks for the suggestion. The problem in the CM context is that the vcam motion is sometimes complex and does not always respond linearly as the target's position changes, so while that approach may work for simple situations my preference is to find a more robust fix.
     
    Last edited: Sep 3, 2018
    andracer108 likes this.
  31. Jde

    Jde

    Joined:
    Oct 2, 2011
    Posts:
    139
    Fair enough. The spring calculation we used was non-linear too, so technically it still allows for some error, but the error range was reduced to the very small difference between the sub-fixed-update linear interpolated position, and the position had it been calculated non-linearly at the same sub-fixed-update step. Which was imperceptible. Also, judder would only be visible on the background if it occurred, rather than on the object being tracked.

    Out of interest, what would a more robust fix look like? I can't think of a way to get smooth motion without running the calculation at a consistent framerate.
     
  32. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    35
    Thank you @Gregoryl! Would you mind explaining further on how to get Cinemachine 2.2.8? I want to test things as well.
     
  33. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @andracer108 With a text editor edit your project's Packages/manifest.json file and add this line:
    "registry": "https://staging-packages.unity.com",
    immediately before the "dependencies" line. Then open Package Manager, and you will see the experimental and unpublished content in addition to full-fledged releases. Use at your own risk.

    If you grab the latest 2.2.8-preview.6, you will then need to define CINEMACHINE_EXPERIMENTAL_DAMPING in your player settings. After the script reload, you will get the new damping behaviour.
     
    Last edited: Sep 4, 2018
  34. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @Jde you're right about doing the damping calculations on a fixed framestep. I thought you were suggesting fudging the vcam's position between fixedFrames by interpolating.
     
  35. Jde

    Jde

    Joined:
    Oct 2, 2011
    Posts:
    139
    I am suggesting that :)

    If the damping happens on a fixed framestep (which it has to), but the rendered frame lands between those steps, then it needs to interpolate? Otherwise there will be judder? I don't see any other option (when tracking an interpolated target).
     
  36. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Indeed I can interpolate the damped values, but this is not always equivalent to interpolating the vcam transforms.
     
  37. TreborJones

    TreborJones

    Joined:
    Feb 5, 2015
    Posts:
    18
    Thanks for taking a look Gregory.

    That test level still shows jittering if you set the aim to do nothing or set lookahead time to 0. Do you mean it's displaying both transposer damping jitter and lookahead jitter?
     
  38. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Yes it is. The experimental damping in CM 2.2.8 should take care of the damping-related jitter, but not the lookahead-related jitter, although you might be able to smooth that out with a sufficiently high smoothing setting.
     
    TreborJones likes this.
  39. TreborJones

    TreborJones

    Joined:
    Feb 5, 2015
    Posts:
    18
    Hey! Just tried out the 2.2.8-preview.6 version and can confirm it seems to have resolved the main issue with damping on the camera body. Good stuff! :)
     
    Gregoryl likes this.
  40. stevesterrr

    stevesterrr

    Joined:
    Aug 3, 2012
    Posts:
    2
    Hey guys, I know this is kind of an old post, but how would you deal with a rigidbody&rootmotion(set on animated physics) combo. Cinemachine appears smooth but it slightly jitters from time to time. I had also tried the 2.2.8-preivew.6 version and I don't see a big difference.
     
  41. Tecnoato

    Tecnoato

    Joined:
    Nov 4, 2020
    Posts:
    1
    My English is not very good...
    I had this problem.

    Use Cinemachine and access the field body, transpose x, y , z :

    Using Cinemachine;
    then put:

    public CinemachineFramingTransposer _anyname;

    and in the code you access Damping:

    _anyname.m_XDamping = 0.5f;
    or
    _anyname.m_YDamping = 0.5f;
    or
    _anyname.m_ZDamping = 0.5f;