jerky camera over terrain

Hi, so I am having an issue with my camera. When I walk on uneven terrain, like up or down a hill, the camera gets a little jerky. I basically already followed the method in the smooth camera script, and that works fine for flat terrain. How Can I get the addition of the terrain height so that my camera follows that smoothly. I tried simply adding the height of the terrain, but that did not work for me. Any ideas? Thanks!

	void LateUpdate() {
//		_myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
//		_myTransform.LookAt(target);

		if(target != null) {											//as long as we have a target, we can move the camera around them

//				_myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
//				_myTransform.LookAt(target);
				// Calculate the current rotation angles
				//float wantedRotationAngle = target.eulerAngles.y;
				float wantedHeight = target.position.y + height;
		
				//float currentRotationAngle = _myTransform.eulerAngles.y;
				float currentHeight = _myTransform.position.y;
	
				// Damp the rotation around the y-axis
				//currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

				// Damp the height
				currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

				// Convert the angle into a rotation
				//Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
	
				// Set the position of the camera on the x-z plane to:
				// distance meters behind the target
				_myTransform.position = target.position;
				_myTransform.position -= Vector3.forward * walkDistance;//currentRotation * Vector3.forward * walkDistance;

				// Set the height of the camera
				_myTransform.position = new Vector3(_myTransform.position.x, currentHeight + Terrain.activeTerrain.GetPosition().y, _myTransform.position.z);
	
				// Always look at the target
				_myTransform.LookAt (target);
			
		}
		else {//if we do not have a target, try to find it and assign it to the target variable
			GameObject go = GameObject.FindGameObjectWithTag(cameraTagName);
			
			if(go == null)
				return;
			
			target = go.transform;
		}
	}

Just realized that my method of getting the height coordinate of the terrain does nothing…lol.

this actually gets the current height
currentHeight = _myTransform.position.y + Terrain.activeTerrain.SampleHeight(_myTransform.position);

but makes the camera go nuts…
So…any ideas

lerp to the new position instead of just directly setting it?

That way the it kind of glides into it’s new position.

You can even lerp only in the y direction so it doesn’t jerk up and down (which is probably where most of the jerking comes from).

Note - one usually clamps how far from the target position the lerp is allowed to be. This way it doesn’t trail to far behind if you move very fast.

isnt that what I am doing in this line? Thanks for the response

currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

Didn’t see that line, your code is rather cluttered with defunct commented out lines of code.

Well, you are directly setting _myTransform.position.y to currentHeight + Terrain.activeTerrain.GetPosition().y. That might explain why your motion appears jerky. You should add the terrain’s height into wantedHeight prior to the interpolation.

Hey Brian, good point. I am going to try that when I return home. Thanks for the reply and I will let you know if it works

Hey hc. I am struggling with the exact same problem. I also tried a method to avoid terrain but it got jerky as well, it used collision detection to adjust the position of a game object and then tried to make a null transform fly after it with this code:

transform.position = Vector3.MoveTowards(transform.position, target.transform.position, smooth);

It sort of worked but got jerky.