I have a script trying to get the gun aim at the target instead of the origin:
It's partially working.
but when I get too close with the mouse the player starts spinning
I would appreciate a help regarding why it happens and how to fix it.
image_angle (the current rotation) to calculate the new rotation for your character, this is what's making it spin - it just keeps summing every frame.
If your goal is to make the character's weapon point at the mouse, take a look at the answers here.