Difference between move_toward() and lerp()

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

So, I’ve recently started learning GDScript, Trying to achieve a platformer-like movement, I had two ideas of doing it, The first one is using this:

   // All variables and constants are set
   func _physics_process(delta):
       var input = Input.get_action_strength("RIGHT") - Input.get_action_strength("LEFT")
       if input != 0:
           velocity.x = move_toward(velocity.x, input * MAX_SPEED, ACCELERATION)
           velocity.x = move_toward(velocity.x, 0, FRICTION)
       velocity = move_and_slide(velocity, Vector2.UP)

This code is working perfectly fine, HOWEVER, When I replace the:

velocity.x = move_toward(velocity.x, input * MAX_SPEED, ACCELERATION)


velocity.x = lerp(velocity.x, input * MAX_SPEED , ACCELERATION)

It just doesn’t work and the player starts teleporting around, This bring us to my question, Why is that? I thought that lerp and move_toward did pretry much the same thing but I think I was mistaken?

Thanks in advance.

:bust_in_silhouette: Reply From: maxwellc055

Hey! I just started earning gdscript/godot a couple months ago, so if anyone sees this and has corrections to make, PLEASE do so.

It can be seen in the godot docs that the third argument passed to the lerp method (float weight) actually represents its portion of the way between the first two arguments. For example lerp(5, 9, 0.75) would return a value of 8.

This can be compared with move_toward in which the third argument passed (float delta) represents the difference between the first argument and the value returned (in the direction of the second argument). For example move_toward(5, 9, 0.75) would return 5.75.

In your code, using lerp. Assume MAX_SPEED = 12, ACCELERATION = 2 and that I am holding down the “RIGHT” input key.

First lerp(0, 12, 2) will return 24 to velocity.x
Next lerp(24, 12, 2) will return 0 to velocity.x
Again, lerp(0, 12, 2) will return 24 to velocity.x

Hypothetically a decent effect could be created using your lerp code, so long as ACCELERATION is less than zero, though your position curve would look more like sqrt(x) than x^2 (concave down, rather than up) which would be pretty unnatural AND you would never technically reach MAX_SPEED, just infinitely approach it. An intermediate effect with an S shaped curve might be a really interesting thing to explore!

I hope I was of some help to you. Good luck!

Thank you so much, That explains why my character was teleporting around when I used lerp() lol

JCNegar | 2020-09-08 14:11

Happy to help.
I’m in the docs every five minutes when I’m trying to program haha

maxwellc055 | 2020-09-11 19:44

:bust_in_silhouette: Reply From: jak6jak

Lerp works differently than move_toward specifically the last argument. the last argument is the weight. Think as this as a percentage. This means if I had a simple lerp set up like this:

var from =10
from = lerp(from,0,.5)

it would always print 5 because 5 is half of 10. The .5 is telling the lerp to find the half-way point between from and to. If it was 1 it would return the “to” value and if it is 0 it would return the “from” value.

While in the move_toward the last parameter is a delta. so we are moving towards the to value in increments. so changing the example above:

var from = 10
from = move_toward(from,0,.5)

The result would be 9.5

Hopefully this clears up any confusion!

Thanks a ton, That helped so much!

JCNegar | 2020-09-08 14:10