I am currently working on a wave survival shooter game and I came across a problem with the player's aiming. When the player aims with the mouse, the character rotates properly as seen in the picture below:

As you can see, the character rotates based on the direction from the position of the tip of the gun to the position of the mouse. It's working quite nicely. But once I aim the mouse close to the character, the character starts to twitch or spin all over the place.

I suspect it has to do with there being some kind of blind spot from the gun to the character. Since the mouse is going behind the gun, it can't point towards the mouse anymore, so it tries but just keeps spazzing out instead. I don't understand it enough and am not sure how to fix this. Here is my player code:
extends KinematicBody2D
var viewport
var screen_size
var draw_node
# Player variables
export var walk_speed = 25 #pixels per second
var mouse_position = Vector2.ZERO
var gun = null
var gun_muzzle = null
func _ready() -> void:
screen_size = Vector2.ZERO
viewport = get_viewport()
screen_size = viewport.size
draw_node = get_node("../Draw")
gun = get_node("Gun")
gun_muzzle = get_node("Gun/Position2D")
set_process(true)
func _process(delta: float) -> void:
var move_direction = Vector2.ZERO
mouse_position = viewport.get_mouse_position()
var look_direction = mouse_position - gun.global_position#mouse_position - position
var look_dir_length = look_direction.length()
look_direction = atan2(look_direction.y, look_direction.x)
# Gets input from the player and stores the corresponding keys as integer values converted from bools
var left_key = int(Input.is_action_pressed("move_left"))
var right_key = int(Input.is_action_pressed("move_right"))
var up_key = int(Input.is_action_pressed("move_up"))
var down_key = int(Input.is_action_pressed("move_down"))
var h_dir = right_key - left_key
var v_dir = down_key - up_key
# Normalize the movement vector
move_direction = Vector2(h_dir, v_dir).normalized()
rotation = look_direction - deg2rad(90)
# Update the player's position
position += move_direction * walk_speed * delta
# Emit signal for drawing a line from the player to the mouse
draw_node.emit_signal("draw_from_player_to_mouse", gun_muzzle.global_position, mouse_position)
Here is what the player's node looks like:

and the gun scene, which I make use of a Position2d node for the tip of the gun:

Thank you for your consideration!