Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. 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
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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:
    25
    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:
    1,217
    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:
    25
    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:
    25
    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:
    1,217
    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:
    25
    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:
    1,217
    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:
    25
    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:
    1,217
    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:
    25
    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:
    1,217
    Well, if it works with the sample scenes but not with your project, I guess 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?
     
  12. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    25
    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:
    1,217
    @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 at 5:30 PM
    andracer108 likes this.
  14. andracer108

    andracer108

    Joined:
    Dec 12, 2016
    Posts:
    25
    @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:
    1,217
    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:
    25
    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.