- Home /
 
Look at mouse problem
I hava a problem i am making a Top Down game and i have a space ship that has a turret on top i have this script to make the turret look at he mouse position
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;
 mouseY = 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);
  
               } 
I attacked this to the camera and the fpc is set as the turret but when i play the game my turret also looks up and down i only want it to look around can someone please help?
Thanks.
Answer by Kourosh · Apr 21, 2011 at 12:39 AM
This might work if I understood your question well:
 worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif)); var turretLookDirection:Vector3 = Vector3(worldPos.x,fpc.transform.position.y, worldPos.z); // here instead of Y value you might put something which suites your game better
 
                fpc.transform.LookAt(turretLookDirection); 
 
               
               Now it should only rotate around, and not looking up/down.
Your answer