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

Question Question on Jitter at High Speeds With Damping

Discussion in 'Cinemachine' started by xorblax, Jul 31, 2023.

  1. xorblax

    xorblax

    Joined:
    Jul 15, 2018
    Posts:
    25
    Hey all, so I've had trouble with the camera jittering when following objects at high speeds for a while and I had decided to spend some time digging into how damping works. Now I consider myself an amateur at all this, but is it possible that the damping equations just aren't accounting for the velocity of the target position? I've written my own extension that substitutes the default framing transposer with my own that has a damping solution which accounts for the target velocity (by just storing its position from the previous frame), and... it works. Much smoother damping no matter how fast I'm going.
    I found a forum thread discussing damping somewhere (wish I had it bookmarked) and got this method from it, which is what I used:
    Code (CSharp):
    1. Vector3 DynamicLerp(Vector3 a, Vector3 b0, Vector3 b1, float damp)
    2.     {
    3.         float dt = damp / Time.deltaTime;
    4.         Vector3 f = a - b0 + (b1 - b0) * dt;
    5.         return b1 - (b1 - b0) * dt + f * Mathf.Exp(-1/dt);
    6.     }
    I think it's definitely true that a damping solution that treats the target position as not moving will result in jittering when the target is moving at high speeds. And if I'm wrong and accounting for target velocity is how it's implemented, then I guess this thread is news that it works when written as an extension?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Interesting. Can you send a simple project that shows this in action, so that I can understand what you're talking about?
     
    xorblax likes this.
  3. xorblax

    xorblax

    Joined:
    Jul 15, 2018
    Posts:
    25
    Yes I can. In it the ball is flying to the right at speeds typical for my game. I've got two virtual cameras, one with the regular framing composer and another that has my extension doing it- you should see a big difference in their jittering. I also have an enum on the extension that lets you switch between the static and relative damping.
    The ball also teleports back to 0,0 when it goes over 5000 units to avoid jitter from floating point stuff.
     

    Attached Files:

    Last edited: Aug 3, 2023
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Thanks for that, but I'm not noticing a significant difference in the jitter. Both cameras exhibit jitter when the framerate is uneven. The Unity editor is especially prone to uneven framerates, so I made a build and tested it there. I added a small behaviour to enable your camera while the mouse button is pressed, so I could switch back and forth. Both cameras are steady.
     
  5. xorblax

    xorblax

    Joined:
    Jul 15, 2018
    Posts:
    25
    It might be because the extension defaults to the static damping option, whoops on my part... if you set this it should be smooth.
    upload_2023-8-3_8-53-45.png
     
  6. xorblax

    xorblax

    Joined:
    Jul 15, 2018
    Posts:
    25
    Here is a demonstration video as well