CharacterBody2D
Part of the Input and Movement section of Coddy's Godot journey. Lesson 7 of 28.
A Node2D goes through walls. A CharacterBody2D bumps into things. In the Scene tab, the Hero is now a CharacterBody2D. Its CollisionShape2D child is a circle, the hero's solid size. The walls and the pillar have collision shapes too.
A body has a velocity, a Vector2 in pixels per second. Setting it moves nothing. move_and_slide() moves the body by its velocity for one frame and stops it at anything solid:
func _physics_process(delta):
velocity = Vector2(150, 0)
move_and_slide()Move bodies in _physics_process, which runs in step with the physics. move_and_slide() applies the frame's time, so never multiply velocity by delta.
Challenge
EasyThe hero's velocity is set to 150 pixels per second to the right. Call move_and_slide() so it moves and stops at the pillar.
Try it yourself
extends CharacterBody2D
func _physics_process(delta):
velocity = Vector2(150, 0)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground