Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

rotation facing mouse unity

//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);
 
PREVIOUS NEXT
Tagged: #rotation #facing #mouse #unity
ADD COMMENT
Topic
Name
9+9 =