Hero: Face the Way
Part of the Input and Movement section of Coddy's Godot journey. Lesson 26 of 28.
The hero still faces right when it runs left. Flip its Look node the way the keys point:
if direction > 0:
$Look.scale.x = 1
if direction < 0:
$Look.scale.x = -1Put these lines after direction is set. With no key held, neither if runs, so the hero keeps its last facing.
Challenge
EasyMake the hero face the way it runs.
- Before
move_and_slide(), set$Look.scale.xto1whendirection > 0. - Set it to
-1whendirection < 0.
Press Play and run left: the hero turns left and keeps facing left when you stop. Then Check.
Try it yourself
extends CharacterBody2D
var speed = 220
var acceleration = 1200
var friction = 2400
var gravity = 1000
var jump_velocity = -500
func _physics_process(delta):
velocity.y += gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
var direction = Input.get_axis("move_left", "move_right")
if direction == 0:
velocity.x = move_toward(velocity.x, 0, friction * delta)
else:
velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
move_and_slide()
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground