Menu
CoddyTech

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 = -1

Put these lines after direction is set. With no key held, neither if runs, so the hero keeps its last facing.

challenge icon

Challenge

Easy

Make the hero face the way it runs by setting $Look.scale.x to 1 or -1. When you stop, it keeps facing the same way.

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()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Input and Movement

Practice on your own: Godot playground