How to do a quick 180 in 3d?

Godot Version 4.3 stable

I’m kinda new to programming and to godot, I’m making a RE clone, so I want to be possible to make the player do a quick 180 when back and sprint is pressed, but I cant find a way to make it smooth, I read about lerp_angles but couldn’t make it work.

Thanks for the help!

Here’s the code I have:

if Input.is_action_just_pressed(“backward”) and Input.is_action_pressed(“sprint”):
rotate_y(deg_to_rad(180.0))

Usually, for smoothing values’ transition out, I like to use Tweens; it’s very powerful ^^

if Input.is_action_just_pressed(“backward”) and Input.is_action_pressed(“sprint”):
    var t := create_tween()
    # Change it to - PI if you prefer that way (btw PI = 180°)
    var goal_rot := rotation.y + PI
    t.tween_property(self, "rotation:y", rotation.y + PI, 0.1)
    t.play()

However, I fear that this section you posted is in the _process or _physics_process, which means that this tween will keep start the smoothing but never get it until the end since it plays every frame (so no smoothing, worse, no rotation actually being done). As such, in this scenario I’d recommend to avoid defining a tween locally, but rather do it in a wider scope:

extends CharacterBody3D # Since your player is probably a CharacterBody3D

...

var rotation_tween

...

# Or _physics_process, idk how you structured your code
func _process(delta):
    if Input.is_action_just_pressed(“backward”) and Input.is_action_pressed(“sprint”):
        if rotation_tween == null or not rotation_tween.is_running()
            rotation_tween = create_tween()
            # Change it to - PI if you prefer that way (btw PI = 180°)
            var goal_rot = rotation.y + PI
            # Put in whatever feels good
            var duration = 0.1
            rotation_tween.tween_property(self, "rotation:y", goal_rot, duration)
            rotation_tween.play()
            # Used to wait that the tween finishes before the following code is executed, add it only if needed
            await rotation_tween.finished 
2 Likes

Thanks very much, it worked wonderfully.

1 Like

Glad that it helped! ^^

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.