Undo Z rotation caused by rotating X and Y

I’m trying to achieve a 3rd person camera which allows me to look up/down/left/right.

Here is my camera script:

look_x.transform.Rotate (_turnSensivity * -Input.GetAxis ("Mouse Y") * Time.deltaTime, _turnSensivity * Input.GetAxis ("Mouse X") * Time.deltaTime, 0);

I use it to lok up and down. Sadly, the Z rotation changes anyway and I end up with this:

The camera’a z rotation changes and there doesn’t seem to be a direct way to modify it. Here is what I tried:

look_x.transform.rotation = Quaternion.Euler(look_x.transform.rotation.x, look_x.transform.rotation.y, 0);

I tried to reset the Z rotation to zero, but then the camera doesn’t rotate at all. Both of these are located in the Update function like this:

void Update () {
		look_x.transform.Rotate (_turnSensivity * -Input.GetAxis ("Mouse Y") * Time.deltaTime, _turnSensivity * Input.GetAxis ("Mouse X") * Time.deltaTime, 0);
		look_x.transform.rotation = Quaternion.Euler (look_x.transform.rotation.x, look_x.transform.rotation.y, 0);
}

How do I undo the Z rotation, but allow the X and Y to be Rotated freely? Do I have to have 2 separate gameobjects for X and Y? If so, how do I get the camera to get the rotation from both?

You should use two objects in order to control the x and y axes separately, such as shown by the standard MouseLook script in the first person controller package.

If you want the player to look up and down, I think you are supposed to rotate only the z-axis and leave x and y alone. The first part of code should be:

 look_x.transform.Rotate (_0, _0, turnSensivity * Input.GetAxis ("Mouse Y") * Time.deltaTime);

Everything should be fine after you try this.

By the way, if the player has a rigidbody attached, you can always set axis to be fixed in the editor mode.