public void FollowUpdate()
{
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
float deltaAngle = wantedRotationAngle - currentRotationAngle;
if (deltaAngle > 180.0f) deltaAngle -= 360;
else if (deltaAngle < -180.0f) deltaAngle += 360;
if (deltaAngle > 90) deltaAngle = 180 - deltaAngle;
if (deltaAngle < -90) deltaAngle = -180 - deltaAngle;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, currentRotationAngle+deltaAngle, 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);
//Quaternion r = Quaternion.Euler(0, rotate, 0);
//float currentRotation = 1;
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x,currentHeight,transform.position.z);
// Always look at the target
transform.LookAt(target);
}