Home

Published

- 1 min read

rotation facing mouse unity

img of rotation facing mouse unity

The solution for this is noted below

rotation facing mouse unity

Solution

   //Rotate Object to face Mouse (2D, Rotate on Z-Axis)

Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

Try other methods by searching on the site. That is if this doesn’t work