# gimble lock and rotation problem

 0 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); } `````` more ▼ asked Jun 18 '11 at 09:49 PM jacob johnston -3 ● 8 ● 9 ● 10 SisterKy 2.4k ● 33 ● 41 ● 59 If you could post your current code, it would help. Jun 18 '11 at 10:39 PM Chris D [code added to post] Jun 18 '11 at 10:45 PM jacob johnston Ok, so you're locking when you do what? Jun 18 '11 at 11:32 PM Chris D the spaceship which i want to rotate towards the mouse. only turns 180 degrees (not even that much). it faces downward (nose of ship pointing towards bottom of screen) and from there will only rotate 90 degrees to the left or right. Jun 18 '11 at 11:42 PM jacob johnston One somewhat off-topic comment: would you please remember to approve the correct answer on the questions you ask? Looking through your previous entries, you haven't accepted a single one as a solution. It will help people with similar problems find their solution faster if you acknowledge that someone's provided a solution to you. Jun 18 '11 at 11:56 PM Chris D add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

2 answers: sort voted first
 0 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. more ▼ answered Jun 18 '11 at 11:48 PM Chris D 2.5k ● 5 ● 7 ● 25 You can restrict the rotation to X axis fixing the Y coordinate of worldPos. If you do this: `````` worldPos = camera.ScreenToWorldPoint(... worldPos.y = fpc.transform.position.y; fpc.transform.LookAt(worldPos); ``````your spaceship will be kept completely horizontal. If you want it to incline a little to the ground, just subtract some constant value from worldPos.y before fpc.transform.LookAt. NOTE: as @ChrisD commented, please click the green button if his answer solve your problem! Jun 19 '11 at 12:34 AM aldonaletto add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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 inspectorprivate 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); ``````} more ▼ answered Jun 19 '11 at 12:42 AM jacob johnston -3 ● 8 ● 9 ● 10 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Topics:

x2171
x181
x61
x9

asked: Jun 18 '11 at 09:49 PM

Seen: 1398 times

Last Updated: Jun 19 '11 at 12:42 AM