Trying to eliminate jitteryness when moving

Hello, I’m a brand new beginner as of yesterday and I’m trying to manually program a smooth camera. The original code for the smoothed camera script goes as follows.

using UnityEngine;

public class CameraFollow : MonoBehaviour
{

    public Transform target;

    public float smoothSpeed = 0.05f;
    public Vector3 offset;
    public Vector3 velocity = Vector3.zero;

    void LateUpdate()
    {
        Vector3 desiredPosition = target.position + offset;
        Vector3 smoothedPosition = Vector3.SmoothDamp(transform.position, desiredPosition, ref velocity, smoothSpeed);
       
        transform.position = smoothedPosition;
       
    }

}

And it worked perfectly with no jitteryness, very smooth camera.

But now, after noticing at extreme speeds the camera “disconnects” from the player since it lags behind too much, I wanted to try and create a function that makes the smoothSpeed smaller the faster you go, thus at extreme speeds the camera no longer lags too far behind the player. To do so I created a new variable newSpeed that became a function of the magnitude of velocity, newSpeed = velocity.magnitude^-2.

The following code shows such:

using UnityEngine;

public class CameraFollow : MonoBehaviour
{

    public Transform target;

    public float smoothSpeed = 0.05f;
    public Vector3 offset;
    public Vector3 velocity = Vector3.zero;
    public float speed;
    public float newSpeed;

    void LateUpdate()
    {
        Vector3 smoothedPosition;
        speed = velocity.magnitude;
        newSpeed = 1 / (speed * speed);
        Vector3 desiredPosition = target.position + offset;
        if (smoothSpeed <= newSpeed)
        {
            smoothedPosition = Vector3.SmoothDamp(transform.position, desiredPosition, ref velocity, smoothSpeed);
        }
        else
        {
            smoothedPosition = Vector3.SmoothDamp(transform.position, desiredPosition, ref velocity, newSpeed);
        }
       
        transform.position = smoothedPosition;
       
    }

}

It runs without error, but now I notice that once my little cube starts moving fast enough, the camera starts jittering around as if I were using Vector3.lerp instead of Vector3.SmoothDamp.

Now I’m a brand new beginner, and I understand that the way I’m doing this might not be the most efficient way, but I want to learn by trying stuff like this out on my own. What I want to understand is:

  1. Why am I getting a jittery effect when I start moving fast enough

  2. How do I make it so it does not jitter anymore

While I appreciate responses that tell me a better way to script camera movement, I also want to understand the core of the issue (the jitteryness) so that I can avoid it in the future as I learn.

Here

        speed = velocity.magnitude;
        newSpeed = 1 / (speed * speed);

A suggestion is to take the distance between the target and the camera instread the current velocity, the velocity vector value from smoothDamp can’t guarantee the smoothness that you want to use as smoothness value