Incorrect mouseLook behaviour

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By b2przemo

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);
}

What kind of game is this? Third Person controller? FPS?

juppi | 2022-09-01 14:40

it is a third-person controller type. The player is in the middle of the viewport. without movement, its rotation works perfectly, but after moveAndSlide (just a few units), its rotation starts working incorrectly. the player should rotate along Y-Axis towards the mouse cursor but instead starts to rotate along the X and Z axis in some kinda unexpected results and the function (code above) that rotate the player to the mouse position doesn’t capture the correct coordinates from the viewport. If I start moving the player back to the world center (0,0,0) the player again rotates correctly (along Y-Axis).

b2przemo | 2022-09-01 15:09

:bust_in_silhouette: Reply From: b2przemo

I found where the problem was. I Normalized() the position that I want to lookAt. This normalization creates the problem. Everything is working fine now.