Sword getting crooked

In my game there is the player and this player has a sword.

This sword rotates following the mouse cursor, that is, the sword only rotates 360 degrees following the mouse.

But when this sword touches somewhere that has collided with the isTrigger OFF, it ends up changing the position values and ending up being crooked and even far from the player.

I was wondering, how could I fix this problem.

Here is the code:

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

public class Lamina : MonoBehaviour
{
    public bool pivotLamina = true;
    public float dano;

    void Update()
    {
        if(pivotLamina == true)
        {
            Vector2 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
            float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
            Quaternion rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
            transform.rotation = rotation;
        }
    }
}

Thank you very much in advance.

So the code you’re showing is actually working correctly, but the problem occurs when something else happens, like some collision occurs, and then the sword flies off.

By considering the code that you haven’t shown?

This is just a red herring, you are looking at the wrong place, instead of proactively identifying where the bug is. From this alone I can tell this was the hard part for you, and now you’re afraid that this might be the problem. Well the problem is that I can’t see any problem with this code, and so you might be overcompensating.

Yes I’m beginning to sound like Dr. House.

Well, depending on your exact setup in the hierarchy, maybe you can try using localRotation instead.
That’s all I can tell from this.