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);        

1 Answer 1


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.

  • \$\begingroup\$ 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 ? \$\endgroup\$
    – Dusty
    Feb 21, 2012 at 15:26
  • \$\begingroup\$ 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. \$\endgroup\$
    – Dusty
    Feb 21, 2012 at 15:41
  • \$\begingroup\$ @Dusty what was "the problem"? \$\endgroup\$
    – Tetrad
    Feb 21, 2012 at 17:03
  • \$\begingroup\$ the Camera needs to be centered above the Object \$\endgroup\$
    – Dusty
    Feb 22, 2012 at 18:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .