Player look at mouse direction 3D Isometric control

Godot Version



I want the player to always look at the position of the mouse and the walking with keyboard. I have made a somewhat functional prototype following tutorials but i have a bug where when the mouse hoovers closer to the player the player starts to rotate on the wrong axis. It sounds like a easy fix but i cant manage to fix it and wonder if there is a completely different way to create a player control like this.

this is the code for the mouse:
func mouseRay():
var spaceState = get_world_3d().direct_space_state
var mousePosition = get_viewport().get_mouse_position()
var rayOrigin = camera.project_ray_origin(mousePosition)
var rayEnd = rayOrigin + camera.project_ray_normal(mousePosition) * 2000
var parameters = PhysicsRayQueryParameters3D.create(Vector3(rayOrigin), Vector3(rayEnd))
parameters.exclude = [self.get_rid()]
var rayArray = spaceState.intersect_ray(parameters)

if rayArray.has("position"):
	var mPosition = rayArray["position"]
	mPosition.y = 0
	var distCam = mPosition.distance_to(global_transform.origin)
	if distCam > 0.3:
		look_at(mPosition, Vector3.UP)