Update 'velocity' with move_and_slide

`move_and_slide` returns the modified velocity, which should be updated in the code. If this is not done, `velocity` will keep increasing due to `velocity.y += delta * gravity`. If one adds a `jump` function to this code it'll not be able to jump, since the vector continues to increase in magnitude and never stops.

By updating the velocity with `move_and_slide` we make sure velocity is 0 when there's no movement, thus reflecting the actual velocity of the object.
This commit is contained in:
videlanicolas
2022-10-09 18:17:31 -07:00
committed by GitHub
parent 7ee27bf7d1
commit 926991cb4c

View File

@@ -210,7 +210,7 @@ This adds simple walking support by pressing left and right:
# The second parameter of "move_and_slide" is the normal pointing up.
# In the case of a 2D platformer, in Godot, upward is negative y, which translates to -1 as a normal.
move_and_slide(velocity, Vector2(0, -1))
velocity = move_and_slide(velocity, Vector2(0, -1))
.. code-tab:: csharp
@@ -245,7 +245,7 @@ This adds simple walking support by pressing left and right:
// The second parameter of "MoveAndSlide" is the normal pointing up.
// In the case of a 2D platformer, in Godot, upward is negative y, which translates to -1 as a normal.
MoveAndSlide(velocity, new Vector2(0, -1));
velocity = MoveAndSlide(velocity, new Vector2(0, -1));
}
}