unity 2d rotate gun to mouse location code example
Example 1: how to make an object point towards the mouse in unity
Vector2 mousePosition = new Vector2 (
Input.GetAxis("MouseX"),
Input.GetAxis("MouseY")
);
transform.LookAt(mousePosition);
Example 2: Point to mouse 2D Unity
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotation_z = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotation_z + offset);