I want a weapon to rotate around the player by mouse postion

I have been trying to make my weapon rotate around the player based on were the mouse is. Its kina like this: 200331-example.gif

The closest I could get is rotating the weapon in place and not around the player I put the weapon as a child of the player and put this script in the weapon. Here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttackArea : MonoBehaviour
{ 
   void Update()
   {
        Vector2 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        Quaternion rotation = Quaternion.AngleAxis(angle - 10, Vector3.forward);
        transform.rotation = rotation;
   }
   
}

I would be thankful for your help!

(Duplicate)

If you think about it, a door doesn’t rotate about itself, it rotates about a hinge. So you create a hinge, make the door a child of that hinge and then rotate the hinge, not the door.

What you want is for the weapon to rotate about a hinge that is located at the centre of the player. Your weapon therefore needs to be a child of this hinge. Create an empty object called Hinge and make it a child of the player. Position it so that Hinge is in the right place, possibly at elbow height but in the centre of the players body. Make the weapon a child of Hinge and then rotate the hinge.

That way, the hinge and weapon will move with the player and the weapon will rotate about the player.