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;
}
}