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: onMain, 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
EasyStart the Coin Collector: make the player walk.
- In
player.gd, setvelocitytoInput.get_vector("move_left", "move_right", "move_up", "move_down")timesSPEED. - Then call
move_and_slide(). Leavecoin.gdandmain.gdas 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
6Project: Coin Collector
Coin Collector: WalkCoin Collector: CoinsPractice on your own: Godot playground