I've got two separate scripts that control character movement. The first one is a very basic singleton pattern that rotates the player towards the mouse position. The second script is attached to the kinematicBody and uses a basic function for changing velocity and moving character by the MoveAndSlide method. Nothing special.
The movement works fine, but the problem is with my singleton pattern. If the character is in the zero world position (0,0,0), the rotation is working as expected. After the movement function starts changing player location the lookAt function starts to rotate kinematicBody in weird directions. Below is the _Process function from that singleton that behaves incorrectly. What am I doing wrong?
var spaceState = stage.GetWorld().DirectSpaceState;
var mousePosition =GetViewport().GetMousePosition();
rayOrigin = camera.ProjectRayOrigin(mousePosition);
rayEnd = rayOrigin + camera.ProjectRayNormal(mousePosition) * 5000;
Godot.Collections.Dictionary intersection = spaceState.IntersectRay(rayOrigin, rayEnd, Exclude, 2);
if (intersection.Count > 0) {
pos = (Vector3)intersection["position"];
pos.y = 0;
Player.LookAt(pos.Normalized(), Vector3.Up);
}