- Home /
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);
}
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.
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.
Answer by Chris D · Jun 18, 2011 at 11:48 PM
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.
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!
Answer by jacob johnston · Jun 19, 2011 at 12:42 AM
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);
}
Your answer
Follow this Question
Related Questions
Limit local rotation around x, y, and z axes? 0 Answers
unwanted rotation on the y and z axes 1 Answer
IMU Sensor and Quaternion 2 Answers
Controlling an asteroid using a rigidbody. 1 Answer
Drift Rotation 1 Answer