+2 votes

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)
       else:
           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)

with:

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.

in Engine by (32 points)

2 Answers

+2 votes

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)
print(from)

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)
print(from)

The result would be 9.5

Hopefully this clears up any confusion!

by (27 points)

Thanks a ton, That helped so much!

+2 votes

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!

by (155 points)
edited by

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.