# Rotation glitch with camera

So I took the SmoothFollow script for my camera, which works pretty well, and wanted to add some kind of Roll (Z rotation) to the camera when it turns to make it more dynamic.

Most of the time my script works well except at a certain angle where it jumps and turns suddenly to ridiculously high values (like 180 when usually I get angles between 15 and -15). I fixed that by limiting the angles to 15 max both ways but it still jumps to 15 and makes it feel a bit awkward. I suspect this angle is the transition between 360 and 0 but I have no idea how to fix that. Here’s the script :

``````var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we
var heightDamping = 2.0;
var rotationDamping = 3.0;
var cameraRoll : float;

// Place the script in the Camera-Control group in the component menu

function LateUpdate () {

var newheight = height + Input.GetAxis( "Mouse Y" );
// Early out if we don't have a target
if (!target)
return;

// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;

var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.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
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

// 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.y = currentHeight;

// Always look at the target
transform.LookAt (target);

var beforeRoll = (wantedRotationAngle - currentRotationAngle)/3; // Takes the values of the camera turning and attenuate them

if(beforeRoll < 15 && beforeRoll > -15 && beforeRoll != 0){
cameraRoll = beforeRoll; // Prevents the angles to go over 15 both way
}

transform.eulerAngles.z = cameraRoll; // Applies the angle
}
``````

Does anyone got any idea what to do?

This is just a guess, but I think it may be trying to go from 359.9 to 361.1, which gets bumped down to 15 by the script fix you did, so I would try making something in function update that jumps that for you instead of having unity do it. Just a guess, but my best try.