Rotate a Game object around its center

Hi, I’am having some troubles with rotating. In my Game the Character has a child GameObject, it’s gun. It’s a top down 2D shooter and I want to shoot and aim with the mouse. I got the aiming working but when rotating the gun it rotates in a big circle and when the mouse is down for example the gun is 2 units away from the Character. I want the exact same feeling of the gun rotating if I would use the rotate tool where it rotates just around the center of the object and doesn’t change its position visually, but only in code of course. I think the tricky bit comes to following the mouse angle but maybe someone someone knows how to fix it, ChatGPT is useless as hell in unity as usual.

— I am a idiot sorry. I just realized that because of the Sprites I drew everything was positioned to high and thats why the big radius when rotating. —

Here is my current code for rotating the gun to look at the Cursor:

void LookAtMouse()
    {
        // Get the mouse position in screen coordinates
        mousePosition = Input.mousePosition;

        // Screen coordinates to world coordinates
        mouseWorldPosition = mainCamera.ScreenToWorldPoint(new Vector2(mousePosition.x, mousePosition.y));

        // Direction from the gun to the mouse position
        direction = mouseWorldPosition - transform.position;

        // Angle between the gun's current forward direction and the direction to the mouse position
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

        // Rotate the gun to face the mouse position
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    }

And this how it looks when my cursor is under the character:

Cheers

To rotate an object around its center, parent your visual object under an empty GameObject and align the visual object as you wish (center it to parent game object in this case), then rotate the empty parent GameObject with your code.

Here’s the method that i use to rotate around a certain point in 2D , same concept applies to 3D just use Vector3.

Just to give a brief explanation, when using quaternions (and rotations generally) the rotation is applied using (0,0) as the pivot to rotate around , to work around this what is done in the code below is we subtract the pivot point from the point p , “temporarily” do the rotation around the center , then add the pivot back.

Think of it as offsetting the whole thing by the pivot , doing your rotation , then canceling that offset to end up with the rotated pointed correctly positioned

/// <summary>
/// Rotate the point p around a pivot by a certain angle
/// </summary>
/// <param name="p"></param>
/// <param name="pivot"></param>
/// <param name="angle"></param>
/// <returns></returns>
public static Vector2 RotateAround(Vector2 p , Vector2 pivot , float angle)
{
    Vector2 p_to_center = p - pivot;
    Vector2 rotated = Quaternion.AngleAxis(angle, Vector3.forward) * p_to_center;
    return rotated + pivot;
}