I’m trying to make a character that aims his weapon at the position of the mouse cursor, and it works, kind of, when the mouse is infront of him (to the right, it’s a 2d game and he faces to the right by default) the weapon “looks” at the cursor, but when he turns to the left (scale X -1 (facing to the right would then be X 1)) it all gets inverted, like, the back of the weapon faces the cursor, so when the cursor is behind him (right side), the weapon points the way he is facing (left side) and when the cursor is at the bottom, the weapon faces up. how do I fix that?
Here’s the code I use:
void Update ()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}