Implementing acceleration in a 2D Platformer for Noobs


How to implement Acceleration by modifying the Code below.
Didn’t find a good youtube tutorial for godot 4.0

And a also have a basic programming logic question:
if direction: ← if directions is what? aren’t this missing something a number for example

and can someone explain my this line
velocity.x = move_toward(velocity.x, 0, speed)
move on the x axis until you reach 0 with speed?
so how to use move_toward with acceleration (or is it irrelevant)

would this work?
var acceleration = 1
var max_speed = 150
velocity.x = direction * acceleration ← But how to stop a max speed?

Any advice is appreciated, I’m just confused

var direction = Input.get_axis("move_left", "move_right")
	if direction:
		velocity.x = direction * speed
		velocity.x = move_toward(velocity.x, 0, speed)

velocity.x means the velocity of the character on the x axis. move_toward means the character will increase/decrease its speed in increments according to the following parameters: velocity.x is the starting point, the second number is the goal velocity, and the last number is the size of the increments. the last number comes default as the constant “speed”, but you can change it to any number you like to change how fast or slow the character will accelerate/decelerate.

1 Like

Maybe something like this…or similar:

if velocity.x >= max_speed:
   then velocity.x = direction
   velocity.x = direction * speed
1 Like