Walk With Velocity
Part of the Input and Movement section of Coddy's Godot journey. Lesson 8 of 28.
To walk with the keys, set the velocity from Input.get_axis() every physics frame, then move:
var speed = 200
func _physics_process(delta):
velocity.x = Input.get_axis("move_left", "move_right") * speed
move_and_slide()Holding right makes velocity.x 200. Holding left makes it -200. Holding nothing makes it 0, so the hero stops when you let go.
With a Node2D you wrote position.x += direction * speed * delta. With a body, velocity is the speed itself. Multiplying it by delta would make the hero 60 times too slow.
Challenge
EasyMake the hero walk left and right at speed. Set velocity.x from Input.get_axis() before move_and_slide().
Try it yourself
extends CharacterBody2D
var speed = 200
func _physics_process(delta):
move_and_slide()
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