Menu
CoddyTech

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 icon

Challenge

Easy

Make the hero move by its velocity.

  1. In _physics_process(), under the velocity line, add move_and_slide().
  2. 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

Practice on your own: Godot playground