Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to make a simple top down game where the player looks at wherever the mouse is pointed. The player is only going to rotate on the Y axis.

Here's what I have so far but I can't seem to get it working.

private void RotateToMouse()
    Vector3 ScreenMouse;
    Vector3 ShipPos;

    //Get Mouse Point ON screen
    ScreenMouse.x  = Input.mousePosition.x;
    ScreenMouse.y  = Input.mousePosition.y;
    ScreenMouse.z  = 1;

    //Get Mouse Point In World
    WorldMouse = Camera.main.ScreenToWorldPoint(ScreenMouse);
    //Get Ship Position
    ShipPos = transform.position;

    //Get Angle Of Mouse From Ship Position
    Angle = Mathf.Atan2(ShipPos.z - WorldMouse.z,ShipPos.x - WorldMouse.x) * 180 / Mathf.PI;

    transform.localEulerAngles = new Vector3(0,Angle,0);        
share|improve this question
up vote 2 down vote accepted

Instead of using Atan2, I would suggest using the built in function Transform.LookAt

So, after you get WorldMouse, just do transform.LookAt( WorldMouse );.

You might have to set WorldMouse.y equal to transform.position.y, but it shouldn't matter.

share|improve this answer
still doesn't work. The object rotates small amounts but Isn't looking at the Mouse, I tried both with and without WorldMouse.y equal to the objects y position. any other suggestions ? – Dusty Feb 21 '12 at 15:26
I Created a new object, and set its position to be WorldMouse. the object does not follow the mouse, I think the problem has something to do with getting the WorldMouse Position from the Screen Mouse. – Dusty Feb 21 '12 at 15:41
Found the problem, Thank you for all the help – Dusty Feb 21 '12 at 16:15
@Dusty what was "the problem"? – Tetrad Feb 21 '12 at 17:03
the Camera needs to be centered above the Object – Dusty Feb 22 '12 at 18:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.