I’m really hoping someone can help me with this. My basic problem is that I have a camera following a ship. The ship can rotate 360 degrees around the X and Y axis. I was using the default SmoothFollow script for the camera, which works great for rotation around the Y axis, but as designed does not rotate around X. So, I tried to adjust it to work similarly around the X axis. However, what happens is once the ship rotates past 90 degrees up or down, the camera following it automatically flips around and everything is messed up.

View a demo of the problem here: http://orgoquest.com/unitytest/rotationtest.html

(Rotate around using the arrow keys, and wasd. Press hold down the up or down arrows and you’ll easily see the problem)

I’m sure this has to do with quaternions, which I still don’t comprehend well, since I see in the inspector the camera rotation starts changing in all 3 planes all of a sudden. Here is the adjusted code I’m using:

```
var target : Transform;
var distance = 10.0;
var height = 5.0;
var heightDamping = 2.0;
var rotationDamping = 3.0;
private var wantedRotationAngleY:float;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
wantedRotationAngleY = target.eulerAngles.y;
wantedRotationAngleX = target.eulerAngles.x;
wantedRotationAngleZ = target.eulerAngles.z;
//wantedHeight = target.position.y + height;
currentRotationAngleY = transform.eulerAngles.y;
currentRotationAngleX = transform.eulerAngles.x;
currentRotationAngleZ = transform.eulerAngles.z;
//currentHeight = transform.position.y;
// Damp the rotation
currentRotationAngleY = Mathf.LerpAngle (currentRotationAngleY, wantedRotationAngleY, rotationDamping * Time.deltaTime);
currentRotationAngleX = Mathf.LerpAngle (currentRotationAngleX, wantedRotationAngleX, rotationDamping * Time.deltaTime);
currentRotationAngleZ = Mathf.LerpAngle (currentRotationAngleZ, wantedRotationAngleZ, rotationDamping * Time.deltaTime);
// Convert the angle into a rotation
currentRotation = Quaternion.Euler (currentRotationAngleX, currentRotationAngleY, currentRotationAngleZ);
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Always look at the target
transform.LookAt (target);
}
```

Thanks in advance for any help.