Godot Version
v4.2.1.stable.mono.official [b09f793f5]
Question
I’m having some rotating a character smoothly. I created this function:
public void RotateTo(Vector3 target, float speed, float delta)
{
var direction = target;
float angle = Mathf.LerpAngle(Rotation.Y, Mathf.Atan2(direction.X, dir.Z), speed * delta);
Vector3 newRotation = new Vector3(Rotation.X, angle, Rotation.Z);
Rotation = newRotation;
}
Which I call in _PhysicsProcess like this
RotateTo(dir, 20f, (float)delta);
dir is a variable which indicates which direction to turn to. Whenever I want the character to turn I just set dir. However I am experiencing some issues.
First, in my movement code, I set dir like this:
Vector3 direction = new Vector3(Velocity.X, 0f, Velocity.Z) * 100f;
direction += GlobalPosition;
dir = -direction;
This works, however it seems that the lenght of the Velocity vector affects the speed of the rotation, which I don’t want.
Second, I have a simple punch set up, and I want the character to turn towards the direction of the punch, obtained from a raycast from the mouse position (I called it direction, but it’s actually a position)
public override void Selection(Godot.Collections.Dictionary dict) //dict is the result of the raycast
{
if (confirm) //confirm is set to true when the player clicks the left mouse button
{
direction = (Vector3)dict["position"];
direction = new Vector3(direction.X, 0f, direction.Z);
SelectionDone = true;
}
}
public override void Use()
{
//User.c.LookAt(direction); //This points to the right direction, but ofc it's not smooth
User.c.dir = -direction;
}
Hovever, this only points to the right direction when the character is at coordinates 0,0.
Does anyone know what I’m doing wrong?