- Home /
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));
fpc.transform.LookAt(worldPos);
}
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
Answer by testure · Jun 03, 2011 at 02:40 AM
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.
also, if you don't know what gimbal lock is, or why it happens- here's a useful video explaining the euler gimbal lock phenomenon:
Answer by jacob johnston · Jun 03, 2011 at 02:57 AM
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
like I said, you need to limit your x rotation to not go past 89 degrees in either direction.
after you take your mouse input and calculate your new rotation, clamp the rotation. IE: $$anonymous$$athf.Clamp(mouseX, -89, 89); // clamp mouseX between -89 and 89 degrees.
here's a video that essentially covers what you're trying to do: http://www.youtube.com/watch?v=ZggxVn93ePI
And lastly- make sure you post comments as comments, not as answers :)
this is all turning out really confusing and hard. is there any way to write a script that just makes the game object face the mouse position along one plane so that it does not tilt up or down? also, to do this without getting the camera involved? something more simple. is this possible, thank you.
Sorry man, I'm not sure how to help you... if you don't know how to edit the script you have, you may be in over your head at the moment. $$anonymous$$y suggestion would be to head over to 3DBuzz and look at their third person camera tutorial.. it's free, and it will explain a lot of the things you're having trouble with right now.
Your answer
![](https://koobas.hobune.stream/wayback/20220612094518im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
transform.LookAt gives a twist 2 Answers
Object Look At Mouse 2 Answers
Neither CursorLockMode.Confined or SetResolution work 0 Answers
Lookat mouse, without raycasting? 1 Answer
Making character look at mouse 1 Answer