Godot Version
v4.3.stable.mono.official [77dcf97d8]
I’m trying to make an NPC rotate towards the position it is going to move. I want to do this with a tween, so that I can make it do other tweens in succession.
I thought this would be easy, but I turned out to be wrong on that. (Or I just don’t get it)
What I tried so far: (In C# by the way)
animation = GetTree().CreateTween();
animation.TweenMethod(Callable.From<Vector3>(RotateNPC), this.Position, targetPosition, 1.0f);
private void RotateNPC(Vector3 target)
{
LookAt(target, Vector3.Up);
}
This just makes it rotate towards the target instantly (so no tween), and then I get errors about the LookAt() target being the same as the current position. I also tried it with this.Rotation but that causes a random rotation movement.
Then I tried to tween the Basis, which sort of works. The only problem is that there occurs some kind of scaling/warping when rotating:
animation = GetTree().CreateTween();
var targetVector = GlobalPosition.DirectionTo(targetPosition);
var target = Basis.LookingAt(targetVector);
animation.TweenProperty(this, "basis", target, 1.0f);
After that I tried another solution I found online: Create a dummy Node3D as a child of the NPC, make that point to the target with the LookAt() method. Then just tween between the current rotation and the rotation of the dummy.
Now the problem is that it sometimes rotates the wrong way round:
animation = GetTree().CreateTween();
rotationDummy.LookAt(targetPosition);
animation.TweenProperty(this, "rotation", rotationDummy.GlobalRotation, 0.5f);
Does anyone have a clue on how to do this properly with a tween?