_physic_process and velocity

godot 4.6.1

Question

following the following course: https://www.udemy.com/course/mobile-game-godot/

I have and issue to understand in chap 2 . mobile gameplay > lesson 3. horizontal Movement, the following line:

velocity.x= move_toward(velocity.x,0, speed)

in the function “_physics_process”:

func _physics_process(delta: float) -> void:
	var direction = Input.get_axis("move_left", "move_right")
	if direction:
		velocity.x = direction * speed
	else:
		velocity.x= move_toward(velocity.x,0, speed)
	move_and_slide()

if there is a move (right or left) is clear to me, but I do not understand why we do not use: velocity.x =0 directly if there is no move ( direction = 0 ) ?

Can you explain me:

  1. why we use move_toward function as in this case we have no move (direction =0)
  2. the velocity.x in the move_toward, is the previous vector2, so the previous x position ? I can understanf from.. to. However in this case I understand it as velocity.x (previous x position) x=0 new position = to. But it would mean that the player is always in movement and never stay at the same x position.

so I’m lost here.

Any help would be appreciate.

thx

it is use to give acceleration. other wise you movement would not be smooth

edit:- for you’re case it is deacceleration

similar to lerp and step functions

edit 2-

In below code you can understand move_toward as the Main function in _process,

generally you’re code is deciding whatever to

1. increase you’re initial value by increment of (you’re third argument in move_toward() function**)**

OR TO

2.decrease you’re initial value (velocity.x) by decrement of (you’re third argument in move_toward() function)

move_toward(first_arg, second_arg, third_arg)

until it becomes you’re desired value

var current_velocity : Vector2 = Vector2(10.0, 0.0);
var final_pos : Vector2 = Vector2(0.0, 0.0);
var acc : float = 10;

func _process(delta : float) -> void:
        #Main function
        if current_velocity.x != final_pos.x:
               if current_velocity.x < final_pos.x:
                     current_velocity.x += delta * acc;
               elif current_velocity.x > final_pos.x:
                     current_velocity.x -= delta * acc;
        #Main function

If a function has “move” in its name, it doesn’t necessarily mean it has to do anything with moving an object.

You can see the description of the function here:

What it does is it adjusts the value of the 1st argument towards the value of the 2nd argument, by the amount of the 3rd argument, while never surpassing the value of the 2nd argument.
So move_toward(1, 10, 3) will return 4, because 1+3.
move_toward(50, 0, 10) will return 40, because 50-10.
move_toward(0, 10, 20) will return 10, because 0+20 would surpass 10, which it can’t by the definition.
Etc…

In your example velocity.x = move_toward(velocity.x, 0, speed) you’re just moving the value from velocity.x towards value 0 by the amount of speed, and assigning the result back to the velocity.x

It’s just simple math, nothing to do with the actual movement of the character.

3 Likes

I read the documentation before asking, but in fact, what confused me was that the “small amount” was speed in both functions… so there is no acceleration or deacceleration in my code, because both have the same “step” which is “speed”.

to understand it, I just replaced speed in the move_toward function by “delta * speed”, to be able to understand that it was deacceleration until 0 (second argument).

that’s why, my first guess was just to set velocity.x = 0.

If in move_toward, the small amount is equal to the “same amount” of the velocity.x (which is in the “if direction:” condition), it is equal to velocity.x = 0, as there’s no deacceleration

The move_toward() function in your example is the acceleration, because it changes velocity over time, not position directly.

1 Like