Top-Down LookAt problem

Hello there,

I am new to Unity and Javascript and am having problems understanding some fundamental concepts. I have watched as many tutorials as I could find and scoured these forums. Although I have found many questions about “Top-down Rotation” no answers have really helped me so please forgive me if this has been answered before.

I have a simple stage with my character on it and a fixed camera facing down. What I am trying to figure out is how to (simply) have my character face the mouse cursor, rotating around the Y-axis.

This is the script I have figured out so far:

var mouseX : int;
var mouseY : int;

function Update ()   {

mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;


My character does rotate a bit, but not 360 degrees. I’m just trying to figure out what I’m doing wrong here. Any explanation on how the code works would be greatly appreciated as I’m having trouble grasping the fundamentals here (boy i feel like a noob).

Anyway, thanks in advance for your answers.

You are confusing two different spaces here. Input.mousePosition is in screen space, between (0,0,0) and (Screen.width, Screen.height, 0), when your player with the Transform component is in world space, which is unlimited (kind of). Now, You have two ways (that I think of now) to do what you want to achieve, to find the direction. It must be done in a single space.

  • Screen space : Assuming your character is in the middle of the screen, the movement direction will be Input.mousePosition - M, with M = (Scree.width0.5, Screen.height0.5). Then, the direction will be (result.x, 0, result.y)
  • World space : you need the world position of your mouse. Create the ray with Camera.ScreenPointToRay, then find the intersection with the ground / a plane facing up. The direction will be result - transform.position.

Good luck.

OH, and you should Quaternion.SetLookRotation if you use a direction (or look at position + direction)