My MouseLookScript makes the camera keep going back to original rotation

I made a MouseLookScript and when I play and move i can move the camera but there’s always an active force moving the camera back to it’s original state. As far as I’m concerned this script worked in JavaScript. What am I doing wrong? This is the script.

using UnityEngine;
using System.Collections;

public class MouseLookScript : MonoBehaviour {

	private float lookSensitivity = 5;
	private float smoothTime = 0.1f;

	// Use this for initialization
	void Start () {
	// Update is called once per frame
	void Update () {
		float yRotation = Input.GetAxis("Mouse Y") * lookSensitivity;
		float xRotation = Input.GetAxis("Mouse X") * lookSensitivity;

		Quaternion rotation = Quaternion.Euler (xRotation, yRotation, 0);
		transform.rotation = Quaternion.Slerp (transform.rotation, rotation, smoothTime);


Input.GetAxis() returns to 0.0 when the device or button is not being pressed. That means this line is always returning your rotation to Quaternion.Identity (0,0,0) when no buttons is pressed:

Quaternion rotation = Quaternion.Euler (xRotation, yRotation, 0);

One way to fix this problem is to declare ‘xRotation’ and ‘yRotation’ outside of Update() at the top of the file:

float xRotation = 0.0f;
float yRotation = 0.0f;

Then you add/subtract from that rotation in Update():

yRotation += Input.GetAxis("Mouse Y") * lookSensitivity;
xRotation += Input.GetAxis("Mouse X") * lookSensitivity;