How to interpolate over time

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

I have this code in a 3D scene:

func _physics_process(delta):
   var target_vector = global_position.direction_to(player_body.position)
   var target_basis = Basis.looking_at(target_vector)
   $EyeAxis.basis = $EyeAxis.basis.slerp(target_basis, 0.1)

Which rotates an eye to look at the player. I want the eye to slowly rotate over a period of time, say 2 seconds. How would I achieve this? A code example would be greatly appreciated!

:bust_in_silhouette: Reply From: godot_dev_

Something like the below could acheive what you want, by using lerp and inverse_lerp

var ellapsedTime=0 #number of seconds ellapsed since rotation start
var duration = 2 # in seconds

#1 full rotation   in 2 seconds
var startingAngle =0
var endingAngle=360 # changing this to 720 would make 2 rotations in 2 sec

func startRotation():
    set_physics_process(true)
    ellapsedTime =0
func _physics_process(delta):
    ellapsedTime = delta + ellapsedTime #track the time ellapsed
    var progressRatio = inverse_lerp(0,duration ,ellapsedTime ) #time ellapsed ratio (e.g., 0.5 meaning 1 second ellapsed and 1 meaning 2 seconds ellapsed)
    var targetAngle = lerp(startingAngle,endingAngle, progressRatio ) #convert the progress to an angle. So 0.5 would lead to 180 degress

    #here you add the logic to rotate the desired node by targetAngle  degreees