Slowly rotating camera to face same direction as player

Okay, so this will take a little explaining.

In my current project, I have an animated character that has a camera placed slightly behind and to the right of him, much like the camera placement in Resident Evil 4 and 5. When the player is either stationary or walking slowly forward (not running), the camera can move within certain bounds in both the horizontal and vertical directions based on the movement of the mouse, allowing the player to scan the general area in front of and around the character. However, I restrict the camera to face dead forward whenever the character runs or walks backwards. This works just fine, but my problem is that the camera immediately snaps into place, which is sudden and disorienting.

I have tried multiple approaches to making the camera glide smoothly into place, using all the Quaternion functions I could think of: Slerp, Lerp, RotateTowards, LookRotation + Slerp, etc.

If someone could help me find the code that would allow for the camera to move nicely and easily into alignment with the characters rotation, I would be greatly appreciative. I have tried to find something that was already asked that would help me with this, but I canâ€™t seem to find anything that works.

The code I have is as follows:

``````        rotationY += Input.GetAxis ("Mouse Y") * 3.0f;
rotationY = Mathf.Clamp (rotationY, -20.0f, 15.0f);

if (anim.GetFloat ("Speed") < 2.0f && anim.GetFloat ("Speed") >= 0.0f)
{
rotationX += Input.GetAxis ("Mouse X") * 3.0f;
rotationX = Mathf.Clamp (rotationX, -45.0f, 45.0f);
}
else
{
rotationX = Quaternion.Slerp (Camera.main.transform.localRotation, transform.rotation, 1.0f * Time.deltaTime).x;
//This code just causes the camera to snap dead forwards, I don't understand why
}
``````

UPDATE:
Thanks to jenci1990, I figured out the problem. The following code is my final solution:

``````        if (anim.GetFloat ("Speed") < 2.0f && anim.GetFloat ("Speed") >= 0.0f)
{
if (rotationX > 45.0f)
rotationX = rotationX - 360.0f;

rotationX += Input.GetAxis ("Mouse X") * 3.0f;
rotationX = Mathf.Clamp (rotationX, -45.0f, 45.0f);
}
else
{
float camX = Camera.main.transform.localEulerAngles.y;
float rotX = transform.forward.y;
rotationX = Mathf.LerpAngle(camX, rotX, 3.0f * Time.deltaTime);
}
``````

try this:

``````rotationX = Vector3.Lerp(Camera.main.transform.localEulerAngles, transform.eulerAngles, 1.0f * Time.deltaTime).x;
``````