How does lerp work?

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

I have been trying to get the player to move to x point without input from the player. I have tried many things but settled on just the lerp function. but run into a few issues with it so far. I have sort of got it to start working but it will not go up on the y axis.

		direction.y = lerp(global_position[1], hook_position[1], 0.009)
		direction.x = lerp(global_position[0], hook_position[0], 0.009)

I have already made sure that I effectively turn off the gravity in the scene, I also have checked to make sure that the positions for both global_position and hook_position are valid.
(note this is all in 2d)
Thanks for any help you can provide!

One option that you might or might not have tried already is to use a tween for this.

You could add this to your player node:

var tween
func _ready():
    tween = Tween.new()
    add_child(tween)

and then when you want to move automatically (over a duration of, say, 1.5 seconds) you can run this on the player node

tween.interpolate_property(self, "global_position", global_position, hook_position, 1.5)
tween.start()

The syntax for the interpolate_property function is:

(Node with property you want to lerp, Name of property you want to lerp, Initial value, Final value, Duration)

Also, it’s not exactly a lerp, since it eases in and eases out to make the motion not be jerky at the start and finish.

haydenv | 2022-05-04 15:41