How can I get an object to rotate 360 degrees with the mouse?

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!

I assume you are working on 2D;

void Update () {
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
Vector2 forward = (transform.position + Vector3.right) - transform.position;
transform.eulerAngles = new Vector3(0, 0, -Vector2.SignedAngle(direction, forward));
        Debug.DrawRay(transform.position, direction,Color.red);
        Debug.DrawRay(transform.position, forward, Color.green);
	}

Green Line = Vector2 forward, Red Line = Vector2 Direction (a.k.a mouse position).

Change forward’s Vector3.right with initial side that your objects is looking.
120098-screen-shot-2018-07-03-at-75839-pm.png

Alright, thanks!

Was trying for the longest time to figure that out. Could find solutions that went along with mine, but I realize it can’t be done.