All Questions

Community

D
Reptilian_Overlord
posted to: Creating the Enemy

I don't understand this line:

_velocity.y = move_and_slide(_velocity, FLOOR_NORMAL).y


How does this create the results we see ?


It works just fine, but I need to understand exactly what I'm doing.

Thank you in advance for any help.

  • Nathan Lovato replied

    In short, this line allows us to reset the character's vertical velocity to 0 when they hit the ground and prevents them character from falling through the floor.


    Here's a more detailed explanation.

    The move_and_slide function handles collisions and returns the resulting velocity of your character.

    For example, the character is falling and they hit the floor, this will return a velocity vector with the vertical component set to 0.

    We apply gravity to our character every frame to make them fall. When they are on the ground, gravity keeps them in contact with the ground every frame, allowing Godot's KinematicBody2D node to know the character is on the floor.

    Now, we need some way to reset the vertical velocity when the character is on the ground because otherwise, the value would increase until it becomes so large the character would move through the floor.

  • D
    Reptilian_Overlord replied

    Thank you very much.


    The course is fantastic by the way. Much better explanations in general than other courses where they just tell you to copy code. I would like to get the bulk of my Godot learning from one single consistent place and I feel like GDQ might just be it.