Lerping to 270° Keeps Spinning Forever

Godot Version

4.2.1

Question

Unfourtunately I cannot upload the video since I’m a new user, but when I try to rotate a cube for 90°, it generally works, but as soon as the next_rotation is 270°, the cube keeps spinning forever and doesn’t reach the 270° degrees.

extends MeshInstance3D # This is a simple cube mesh in the game.

var next_rotation: float

func _unhandled_key_input(event: InputEvent) → void:
	if Input.is_action_pressed(“rotate”):
	  next_rotation += 90.0
	
		if next_rotation == 360.0:
			next_rotation = 0.0
		
		print(next_rotation)


func _process(delta: float) → void:
   global_rotation_degrees.y = lerp(global_rotation_degrees.y, next_rotation, 0.1)

Try this in game.

That’s why lerp_angle() exists, but you need to change your calculation to be based on radians.
This works to me correctly:

extends MeshInstance3D # This is a simple cube mesh in the game.

var next_rotation: float

func _unhandled_key_input(event: InputEvent) -> void:
	if Input.is_action_pressed("rotate"):
		next_rotation += PI / 2
	
		if next_rotation == TAU:
			next_rotation = 0.0
		
		print(next_rotation)


func _process(delta: float) -> void:
	rotation.y = lerp_angle(rotation.y, next_rotation, 10 * delta)

PS, you don’t even need this part, it will work the same without it. Unless you need to keep your other calculations to be between 0 and 360 degrees for some reason.

		if next_rotation == TAU:
			next_rotation = 0.0
1 Like

Thanks for the help, I’ve been trying to fix it with lerp_angle the entire day yesterday but I couldn’t figure out how the function worked. You are such a help, now it works…

1 Like

Plus I didn’t know you could use PI / 2 to get 90° and TAU for 360. That’s useful knowledge.

1 Like

You can also use deg_to_rad() to achieve the same, then deg_to_rad(90) == PI / 2, deg_to_rad(360) == TAU.

1 Like