Camera shake

I am using the Unity CharacterController Component and it is moving around very well, without shaking. When I parent a camera to it, the camera is following the character and it is not shaking.
Now I have created a script that should calculate the camera position (the camera is not parented any more) and follow the character. But now the camera is shaking/jittering. Here is the code from the camera script:

float distance = 5;
Transform Target; // Camera should follow this target

void LateUpdate() {
        rotationX += Input.GetAxis("Horizonzal")* Sensitivity;
        rotationY += Input.GetAxis("Vertical")* Sensitivity;

        Vector3 direction = new Vector3(0, 0, -distance);
        Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
        Vector3 camPosition = Target.position + rotation * direction;

        float posX = Mathf.SmoothDamp(mainCamera.position.x, camPosition.x, ref velX, X_Smooth);
        float posY = Mathf.SmoothDamp(mainCamera.position.y, camPosition.y, ref velY, Y_Smooth);
        float posZ = Mathf.SmoothDamp(mainCamera.position.z, camPosition.z, ref velZ, X_Smooth);
        mainCamera.position = new Vector3(posX, posY, posZ);

PS: The camera is only shaking on a flat terrain, it is not shaking when I walk over other objects or uneven terrain.

CharacterController is most likely moving with Update or FIxedUpdate.

Change the camera’s movement to the same. (If Player’s movement are executed in Update, change the LateUpdate to Update, in the camera script)…

Check my answer in this thread: