up_direction parameter in godot engine

Viewed 484

I was learning godotengine and I got confused from using up_direction parameter in move_and_slide() method. Actually, I didn't understand what does it do and what is the relation between it and is_on_floor() method.

Thanks in advance.

1 Answers

The up_direction parameter of move_and_slide tells the KinematicBody or KinematicBody2D what direction is up for identifying floor, walls, and ceiling.

As a result of calling, move_and_slide, the object will move, it will collide, and it will classify what it collided with according the angle with the up_direction. Like this:

  • If the collision is roughly in the opposite direction as up_direction, it is a floor.
  • If the collision is roughly in the same direction as up_direction, it is a ceiling.
  • Otherwise, it is a wall.

Then move_and_slide will update is_on_floor, is_on_wall and is_on_ceiling accordingly.

And I say "roughly", because a collision in a direction that diverges less than the value of the floor_max_angle parameter, it will still count.

Furthermore, the floor_max_angle criteria is also used to decide if it will stop or slide the surface it collided with. If it will slide, that changes the velocity vector. Which is why move_and_slide returns a new velocity vector.

It is also worth noting that if you pass ZERO, everything is considered a wall. For example, if you pass Vector2.ZERO as up_direction to KinematicBody2D.move_and_slide, then it will consider everything it collides with to be a wall. Which is useful for top down games

Related