troubles with look at mouse script

i have this script:

//attach this script to your camera and drag your character GameObject in the fpc slot in the inspector

private var worldPos : Vector3;
private var mouseX : int;
private var mouseY : int;
private var cameraDif : int;
var fpc : GameObject;

function Start () {

//determines how far down the ScreenToWorldPoint is from the camera position
//it's calculated [height of camera] - [height of pivot point of character]
//this is to ensure the character only rotates (via LookAt) along rotation.x and doesn't look up or down
cameraDif = camera.transform.position.y - fpc.transform.position.y;


function Update () {

mouseX = Input.mousePosition.x;
mouseZ = Input.mousePosition.y;

//this takes your current camera and defines the world position where your mouse cursor is at the height of your character -->translates your onscreen position of mouse into world coordinates
worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));



it works well and looks at the mouse… most of the time; when the mouse is below the front of the ship on the screen. what i am trying to say is that if the mouse goes above the ship on the screen it will not look at it directly. then the back of the ship looks at its exact opposite position. like if you cut the screen down the middle vertically it would take the mouse position and mirror it on the other side. any suggestion. all help is appreciated thank you

this is called gimbal lock, and why most freelook systems will limit X rotation in some way. Long story short, you don’t want your camera’s X rotation to go past 89 degrees in either direction or you’ll be gimbal locked.

if this is so, do you have any suggestions on a fix to this problem or another link to fix the problem. this is the closest i have gotten to getting the correct effect i wanted in my game. thank you for any help