Martin Häusler
Martin Häusler

Reputation: 7284

How to properly apply a Tween to a 3D rotation in Godot 4?

I have a Node3D which I would like to smoothly transition to a certain target rotation (given in euler angles, in degrees).

When I manually apply lerp(...) on all 3 dimensions of the euler angle vector (modulo 360) then it works fine. However, when I attempt to use a Tween (which seems to be the preferred way for such a use case in Godot 4) then I get crazy intermediate rotations - the final position is again fine.

Am I doing things wrong here? Can anyone give me an example of how to properly rotate a Node3D towards a given target rotation using a Tween?

Upvotes: 2

Views: 1667

Answers (1)

MSDMMM
MSDMMM

Reputation: 61

Came across this question when I had the same problem. This is how I solved it for a simple rotation. For more advanced stuff, you might want to look into quaternion rotations:

# example degrees
var x_degrees: float = 0 
var y_degrees: float = 45
var z_degrees: float = 25

# put degrees in Vector3
var target_rotation = Vector3(
    deg_to_rad(x_degrees),
    deg_to_rad(y_degrees),
    deg_to_rad(z_degrees)
)

#create tween and apply rotation property
var rotate_tween = get_tree().create_tween()
rotate_tween.tween_property(player, "rotation", target_rotation, 0.5) #example time

Upvotes: 2

Related Questions