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)
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.
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…