I’m making a 2D game with a turret of sorts, and I’m trying to get it to rotate in a full circle, following the mouse.
Currently I’m using this method:
xPos = ((gameObject.transform.position.x / 19.2f) * Screen.width);
yPos = ((gameObject.transform.position.y / 10.8f) * Screen.height);
distX = Input.mousePosition.x - xPos;
distY = Input.mousePosition.y - yPos;
ratio = distX / distY;
angle = Mathf.Atan(ratio) * Mathf.Rad2Deg;
gameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, -angle));
This works to an extent. It only rotates along the top half of the circle. If I go down below the “horizon” so to speak, it just goes back to the opposite side. Imagine if, right after the sun sets, it teleports to the East and rises again. In short, I can only rotate in a half-circle instead of a full circle.
Any help is appreciated!