How does this 2D movement system work?

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

Take a look at this kinematicBody2d movement code.

if Input.is_action_pressed(“Right”)
velocity.x += 1

How does Adding (velocity.x) Paired with the symbol (+= ) And with number (1) Makes it move?

:bust_in_silhouette: Reply From: kidscancode

That code doesn’t make anything move. It just adds 1 to the x component of a Vector2 variable called velocity.

Since the word “velocity” means “change in position over time”, that variable is likely being used later in the script to change the object’s position, something like

position += velocity * delta

For example, or some other method, depending on what type of object it is.