Smooth camera height adjustment based on terrain

Hello, I have written the following code for an RTS-style camera, which works “well”, but not exactly how I want.

function LateUpdate()
    var currentHeight = Terrain.activeTerrain.SampleHeight(transform.position);
    transform.position.y = currentHeight + 90;

Basically, it constantly detects the height of the terrain under the camera and keeps it 90 units above it. But it adjusts the height too quickly, thus if the terrain is jittery, the camera will feel jittery (e.g. it’s only as smooth as the terrain is).

Below is an illustration of what I would like, but I’m not sure how to do it. Any thoughts?

It has been a while, but a legit question deserves an answer. May be it’ll be helpful for future problem solvers. I had the same problem and just used Mathf.Lerp to set the new height.

Vector3 camPos = transform.position;
float groundLevel = Terrain.activeTerrain.SampleHeight(camPos);
camPos.y = Mathf.Lerp(camPos.y, groundLevel + 90, Time.deltaTime * responsiveness); // 10 responsiveness results in a good not too lazy movement
transform.position = camPos;