Unity – LookAt 2D – Mouse Position
Instead of ‘Transform.LookAt’ Unity function you can use Mathematic.
1. DRAG AND DROP this script over the object you want to look at target
var mouse_pos : Vector3; var target : Transform; //Assign to the object you want to rotate var object_pos : Vector3; var angle : float; function Update () { mouse_pos = Input.mousePosition; mouse_pos.z = 5.23; //The distance between the camera and object object_pos = Camera.main.WorldToScreenPoint(target.position); mouse_pos.x = mouse_pos.x - object_pos.x; mouse_pos.y = mouse_pos.y - object_pos.y; angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(Vector3(0, 0, angle)); }
2. Inspector> select the target for the variable ‘Target’
3. Play!