Menu
CoddyTech

Coin Collector: Walk

Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 23 of 27.

Over the next five lessons you build Coin Collector. You walk around a room, take every coin, and win when the last one is gone.

The game has three scripts:

  • player.gd: the robot you move.
  • coin.gd: every coin runs this one.
  • main.gd: on Main, the game's rules.

First, walking. The player is a CharacterBody2D. The walls and the two grey blocks are StaticBody2D. You wrote this walker in the last section:

func _physics_process(delta):
	velocity = Input.get_vector("move_left", "move_right", "move_up", "move_down") * SPEED
	move_and_slide()

move_and_slide() moves the body by its velocity and stops it at walls. Changing position yourself would walk through them.

challenge icon

Challenge

Easy

Start the Coin Collector: make the player walk.

  1. In player.gd, set velocity to Input.get_vector("move_left", "move_right", "move_up", "move_down") times SPEED.
  2. Then call move_and_slide(). Leave coin.gd and main.gd as they are.

Press Play: the arrows or WASD walk the player, and the walls stop it. Then Check.

Try it yourself

extends CharacterBody2D

const SPEED = 200.0

func _physics_process(delta):
	pass

All lessons in Signals and Collisions

Practice on your own: Godot playground