I want an object to turn smoothly without holding down a key

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Prilat

Hi, might be a stupid question but i could not find a way to do it

so i want an object to turn 180 degrees smoothly when a key is just pressed , the code below makes the object turn instantly,

if Input.is_action_just_pressed("turn"):
	rotation_degrees.z += 180

i tried to use lerp but it would not work unless i hold or spam the key, again i’m new to godot and gamedev in general,some help would be amazing.

:bust_in_silhouette: Reply From: sash-rc

Do not change angle directly. Instead add variable desired_angle and interpolate rotation to that angle in _process()

Something like that

func _process(delta: float):
  var diff = desired_angle - rotation_degrees
  rotation_degrees = lerp(rotation_degrees, desired_angle, delta * diff * rate)

Thank you! it worked.

Prilat | 2020-12-10 12:16