gimble lock and rotation problem

i am in the progress of making a space game in unity. i have gotten quite a ways into it and have had few problems. my goal is to have the front of the ship always look or aim at where the user places his or her mouse and i want it to rotate along one axis. i have a script on my camera object that carries this function out but i know now that th ship will not rotate fully because of a gimble lock. i have been thinking about how to fix this and tried a few things but none have proved to fix the problem. i am very interested in finding a way to fix this. if anyone knows how to fix this or an alternative method to reach my goal i would greatly appreciate it. thank you so much

/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;

	Mathf.Clamp(mouseX, -89, 89); 
	// clamp mouseX between -89 and 89 degrees.
    //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));

    fpc.transform.LookAt(worldPos);

}

It looks to me that you’re clamping your mouse position between -89 and 89 pixels (as seen on screen). I’m assuming you wanted to clamp the angle it’s facing between the same range instead?

Also,

mouseZ = Input.mousePosition.y;

vs

worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, **mouseY**, cameraDif));

you’re assigning to mouseZ but trying to use mouseY.

thank you so much that fix the problem completely. i have been working on this for a month or so now. thanks, if anyone wants the completed script that fixed the problem it is right here:

//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, mouseZ, cameraDif));
worldPos.y = fpc.transform.position.y;
fpc.transform.LookAt(worldPos);

}