Mouse Rotation Flipping

I am writing my own flight controller and whenever my character rotates upside down the horizontal rotation reverses.

Here is my code:

var Speed = 16;
var turnSpeed = 360;
private var finalRotX : float;
private var finalRotY : float;

function Start() {
	finalRotX = transform.rotation.x;
	finalRotY = transform.rotation.y;
}

function Update() {
	// Rotate The Player
	var rotX = Input.GetAxis("Mouse X") * turnSpeed * Time.deltaTime;
	var rotY = Input.GetAxis("Mouse Y") * turnSpeed * Time.deltaTime;
	finalRotX += rotX;
	finalRotY += rotY;
	transform.rotation = Quaternion.Euler(Vector3(-finalRotY, finalRotX, 0));

    // Move the object forward along its z axis 1 unit/second.
	var hor = Input.GetAxis("Horizontal") * Speed;
	var ver = Input.GetAxis("Vertical") * Speed;
	transform.Translate(Vector3(hor,0,ver) * Time.deltaTime);
}

I’m looking for the reason this happens. Does anyone have any solutions to this? Is their a clean fix that would make this work? It is very annoying.

EDIT: I’ve also noticed a new problem. For some reason, the object has a slight drift to it. Even when no keys are pressed. The dead value of the Horizontal and Vertical axis is set to zero and this still occurs. I have no idea what the problem is.

two words: gimbal lock.

As for your other problem, the drift is likely due to deadzone. Even if it looks like the input is zero, it could have a very miniscule amount of input coming in, so it’s usually good to do a deadzone check anyway. Based on the code you’ve posted that’s pretty much the only explanation. Also make sure your mouse is on a flat even surface or you don’t have a gamepad hiding somewhere on your desk where something might be pushing one of the sticks slightly. That has happened to me on numerous occasions.

Thanks, Yeah, I had my 360 Controller plugged in. I fixed it by setting the rigidbodies speed to zero at the start of the update. I should have it figured out soon.

I tried this script and didn’t notice anything wrong - the character moved like an airplane would do (if it could fly sideways, of course), following its local axes. It only seems to be reversed when coming back, but it’s an observer mistake - if you could mark the wings as L and R, you would see that the left control always makes it go to the L wing side, and vice versa.