SmoothDamp causes horrible jitter

I have written this code for a camera to follow an object:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TargetFollow : MonoBehaviour {
    public GameObject FollowThis;
    public float SmoothTime; //1f

    Vector3 CurrenPos;
    Vector3 TargetPos;
    Vector3 vel = Vector3.one;
    void FixedUpdate (){
        CurrenPos = this.gameObject.transform.position;
        TargetPos = FollowThis.gameObject.transform.position;
        this.gameObject.transform.position=Vector3.SmoothDamp (CurrenPos, TargetPos, ref vel, SmoothTime);
    }
}

When following an object, the camera horribly jitters. I have already tried putting this in Late and Fixed Update, and also set the target’s rigidbodies interpolation to interpolate, but jitter continues. Any idea what is going wrong?

Are you setting smoothtime to 1.0f? Otherwise I think if it is zero it will snap.

Also, why not start out vel at Vector3.zero ? The sample code for this API function does that:

I’ve set vel as Vector3.zero but nothing happened. Also setting SmothTime as 0f clamps but I do not want the camera to be always stuck on target. Moreover setting SmoothTime as 0f produces even more jitter. In fact, I’ve observed that less the value of SmoothTime, more is the jitter

You’ve got something else bumping into it or interfering with smooth operation.

I recreated your basic script and a test scene with a bouncy ball and it is buttery smooth. See attached package.

EDIT: don’t use this package; it throws a nullref, see below for update.

3102369–234242–SmoothDamp.unitypackage (4.09 KB)

I would try LateUpdate and see if that helps at all.

Whoops, made a last minute untested change and the above package has a nullref. Try this one.

3102376–234244–SmoothDamp.unitypackage (4.04 KB)

If @Kurt-Dekker 's response is accurate, I’d investigate if something else is interfering, but still think late update is often preferred. Just check for whatever you think looks better.

I’ve tried this. Everything in your scene works perfect, but in my project, jitter still continues. I think this has something related to Rigidbodies according to this: http://www.kolmich.at/forum/index.php?qa=1228&qa_1=solved-jittering-camera-vehicle-in-a-driving-game

Do you have a rigidbody on the camera? That’s not what you want in this case.

No, there is no rigidbody on the camera

Assuming you use source control or otherwise have good backups, open your scene and start disabling thing after thing until you figure out which one is causing you grief.

There’s only like four or five “things” required to show off the functionality of a SmoothDamp, and those are proven to work in the package I posted.

By deletion or disablement, slowly bring your scene down to the level of complexity of my scene, and when you find what fixes it, look there!

In the project’s Time settings, I changed the Fixed and Maximum allowed timestamp to 0.0166666 and the jittering reduced dramatically, but a little bit of shaking is still present.

I managed to solve the problem.

I had similar problem with my camera follow script.

In my case camera follows player object with rigidbody component on it.
And jitter dissapeared when i choose in rigidbody component Interpolate setting “Interpolate”

2 Likes

Wrestled with this for many days… nothing helped except setting Fixed and Max Time to 0.01666* … my times were set at 0.2 and 0.3 … are those the Unity defaults? Why are they the defaults?

UPDATE: FWIW, I’ve tried release builds with Time.deltaTime and Time.smoothDeltaTime. smoothDelta is definitely ahh… smoother :). I see a lot of talk about just using deltaTime, but in my case at least, it is not smooth. I hope this helps someone.

Also, SmoothDamp uses deltaTime as its default, you have to pass in a smoothDeltaTime or, and again in my experience only, you’ll never get smooth scrolling.

https://forum.unity.com/threads/fixed-timestep-features-coming-to-entities-dots-physics-packages.899687/#post-5916023

No one knows and it doesn’t matter. It is expected to tweaked by the developer.
Also these settings are highly dependent on your framerate and/or on the framerate of your monitor. Fixing jerkyness is not a straight-forward thing and usually there is no global, good solution for everyone in Unity.

1 Like