0 votes

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);
}
Godot version 3.5
in Engine by (18 points)

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

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).

1 Answer

0 votes

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.

by (18 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.