All Questions


Yok-Him Devn

To reset veolcity to zero

Hello Nathan, Truly coding newbie question here. I was wondering why by simply writing "velocity = move_and_slide(velocity)" we would be able to reset the velocity. Here's my take on it: 1. move_and_slide can reset value and it returns a modified parameter which in this case is the "velocity" between the parenthesis. 3. But writing "move_and_slide(velocity)" along doesn't apply it to anything. 2. So by writing "velocity =" we assign the "move_and_slide(velocity)" to the velocity, thus we reset the "velocity". Would appreciate it if you could confirm whether I get this right, thanks!
  • Nathan Lovato replied

    The method move_and_slide internally takes your velocity, moves the character, and if it collides with something, it the function tries to slide the character against the collision surface and calculates a new velocity.

    The function can do this several times in a loop, then, it returns the last calculated velocity, which we can choose to store and use or not. That velocity will not always be of length zero, but if you hit a wall face-on, it should go down to zero.