I will try to help you.
lerp_angle calculates the smallest distance between the current direction and the objective.
I use this on my NPCs when they need to spin smoothly until they are in the right direction.
var t0 = (dest-position).normalized()
rotation = lerp_angle(rotation,t0.angle(),0.1)
if self.get_angle_to(dest) < 0.1 and self.get_angle_to(dest) > -0.1:
This function will stop the rotation.
you just need to define the destination. Something like:
if input is right: dest = position+Vector2(0,10)
elif input is left: dest = position-Vector2(0,10)
This is summarized but I believe you understand kkkk
In short, it would be something like this:
var Status:int = 0
var dest:Vector2 = Vector2(0,0)
func _input(event):
if event.is_action_pressed("ui_right"):
dest = position+Vector2(0,10)
Status = 1
elif event.is_action_pressed("ui_left"):
dest = position-Vector2(0,10)
Status = 1
elif event.is_action_pressed("ui_up"):
dest = position-Vector2(10,0)
Status = 1
elif event.is_action_pressed("ui_down"):
dest = position+Vector2(10,0)
Status = 1
func _physics_process(_delta):
if Status == 1:
var t0 = (dest-position).normalized()
rotation = lerp_angle(rotation,t0.angle(),0.1)
if self.get_angle_to(dest) < 0.1 and self.get_angle_to(dest) > -0.1:
Status = 0
Thank you for your answer, it works perfectly now !
Thanks a lot !