All Questions


Alex Morales
posted to: Creating the Enemy

Trouble wrapping my head around line 12

At around 8:10 of the video, you explain that the move_and_slide value resets the _velocity to zero on collision. However when I attempt to visualize in my head it looks like this. line 10-_velocity is -300 line 11- _velocity is multiplied by -1 to make it go 300 line 12-_velocity is 300 but gets assigned 300? My question is- what is really going on? I've read the docs on move_and_slide and it says it returns a rotated vector if a collision occurs, which only serves to confuse me more sorry if I'm missing something obvious

  • Nathan Lovato replied

    The move_and_slide function takes an input velocity to move the character in a given direction, at a certain speed. That's what a velocity is.

    Internally, the function moves the character and handles collisions, making the character slide a bit against walls or the floor it hits, rather than getting stuck. To do so, it projects the remaining portion of velocity vector after the collision along the collision surface. That's where the "slide" comes from. That only happens when there's a collision, but in that case, the velocity changes inside the function.

    That is why it returns a new velocity. Hitting the floor or a wall should slow down your character. The returned velocity from the move_and_slide function does that: when your entity collides with something, the function will return a different velocity that takes that into account. The velocity in question depends on the angle of the collision and the surface you collided with.

    I hope this helps, tell me if you have more questions!

  • M
    Alex Morales replied

    Thank you! that solves my problem. keep up the good work