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
EasyMake the hero move by its velocity.
- In
_physics_process(), under thevelocityline, addmove_and_slide(). - Indent it with one tab, like the line above.
Press Play: the hero glides right at 150 pixels per second and stops at the pillar. Then Check.
Try it yourself
extends CharacterBody2D
func _physics_process(delta):
velocity = Vector2(150, 0)
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground