Black Week: −40% Pixor is $19.99 $11.99 until 2 December, on itch.io →

8-direction sprites and movement in Godot 4

A character that walks in eight directions needs three things: movement that is as fast on the diagonals as straight ahead, a rule that turns the velocity into one of eight sides, and an animation per side to play. Here is all of it in one script, and where the eight-direction sheets come from.

The crystal golem from eight sides in a row: front, then turning counter-clockwise through its right side and back to the other diagonals
Eight sides of one character, South first
The crystal golem walking on the spot, a dark stone body with pale blue crystals, in pixel art
One side's walk, as the AnimatedSprite2D plays it

The movement

Input.get_vector reads four actions at once and returns a Vector2 whose length is at most 1, so pressing right and down together gives about (0.71, 0.71), not (1, 1). Multiply it by a speed, put it in velocity, and let CharacterBody2D.move_and_slide() move the body and slide it along walls. In Godot 4 move_and_slide takes no arguments; it reads velocity and writes it back after collisions.

Picking the direction

Vector2.angle() gives the angle of the velocity in radians, measured from the right (East). Godot’s 2D y axis points down, so going down the screen is +PI / 2 and going up is -PI / 2. Divide the angle by 45° (TAU / 8), round it, and posmod the result into 0 to 7:

Sector 0 1 2 3 4 5 6 7
Moving right down-right down down-left left up-left up up-right
Side East Southeast South Southwest West Northwest North Northeast

South is the side that faces the player, the character’s front. When the input stops, keep the sector from the last frame you moved in: the character stands facing where it was going instead of snapping back to South.

The animation names

A Godot SpriteFrames resource exported by Pixor has one animation per action and side, named Action_Side: the action as it is called in your project, an underscore, then the side, such as Walk_South, Walk_Northeast or Idle_West. The eight sides are named South, Southeast, East, Northeast, North, Northwest, West and Southwest. Every animation loops and keeps each frame’s hold. The script below builds the name from two exported strings, so it works whether your clips are called Walk or walk.

The script

Make a CharacterBody2D with a CollisionShape2D and an AnimatedSprite2D child, load the exported .tres into the sprite’s Sprite Frames, and attach this to the body:

extends CharacterBody2D

# Sector 0 is East (angle 0), counting clockwise on screen because y points down.
const SIDES := ["East", "Southeast", "South", "Southwest",
		"West", "Northwest", "North", "Northeast"]
const STEP := TAU / 8.0

@export var speed := 80.0
@export var idle_action := "Idle"
@export var walk_action := "Walk"
## Extra degrees the input must turn past a sector's edge before the side changes.
@export var hysteresis_deg := 8.0

@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D

var sector := 2 # South: facing the player

func _physics_process(_delta: float) -> void:
	var input := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	velocity = input * speed
	move_and_slide()

	var moving := input.length() > 0.1
	if moving:
		sector = _sector_for(input.angle())
	var anim := (walk_action if moving else idle_action) + "_" + SIDES[sector]
	if sprite.animation != anim:
		sprite.play(anim)

func _sector_for(angle: float) -> int:
	# Stay on the current side until the angle is clearly past its edge.
	var off := wrapf(angle - sector * STEP, -PI, PI)
	if absf(off) <= STEP / 2.0 + deg_to_rad(hysteresis_deg):
		return sector
	return posmod(roundi(angle / STEP), 8)

The ui_* actions exist in every project, so this runs as it is; swap in your own move_left and friends from the Input Map when you have them. The side comes from the input, not from velocity after move_and_slide, so sliding along a wall does not turn the character to face it.

If you only have five sides, map the three west ones to their east twins and set sprite.flip_h for them, as the AnimatedSprite2D tutorial shows.

Isometric games

In an isometric game the sides were rendered 45° apart on the ground, but on screen a 2:1 diagonal is about 27° from horizontal. Pick the sector from the ground direction, then squash it for the screen: for a 2:1 grid, var screen := Vector2(ground.x - ground.y, (ground.x + ground.y) * 0.5). If you pick the side from the screen vector instead, the diagonals switch sides early. Isometric pixel art covers the camera angles.

3D games with sprites

In a 3D world, put the frames on an AnimatedSprite3D with Billboard set to Y-Billboard, so it always faces the camera but stays upright. The side then depends on where the camera is, not only on where the character walks: take the direction from the character to the camera, turn it into the character’s own space (its global basis, inverted), and snap the angle around the Y axis to 45°. The camera looking at its face is South, its back is North, and the character facing the camera’s right is East. That is how Doom-style sprites work.

Getting the eight-direction sheets

Drawing eight sides of every animation by hand is the slow part. Pixor renders them from a 3D model: in the Scene step, set Ring round the model to 8 and it adds a camera per side, South, Southeast, East and on round, all at one size and one pixel scale. In the Asset graph, add File: Godot SpriteFrames, and Export writes the sheet with a row per side and the .tres beside it. From the command line:

pxr render hero.glb --clip idle --clip walk --ring 8 --export godot

The 8-direction walking character tutorial goes through the render, and From Pixor to a Godot 4 AnimatedSprite2D through the import. If you would rather not write the script, the Godot scene output (--export godot-scene) writes a .tscn with the AnimatedSprite2D, an AnimationPlayer and an AnimationTree whose move state blends idle, walk and run by facing times speed: set parameters/move/blend_position to the facing vector times a speed from 0 to 1.

Common mistakes

  • Up and down swapped. Godot’s 2D y axis points down. A table written for maths class (y up) puts North where South should be.
  • Flicker at the borders. Moving at exactly 22.5° with a little noise on a stick flips between two sides every frame. The hysteresis above keeps the current side until the input is clearly past the edge; idle keeps the last side.
  • Faster diagonals. Building the vector from four is_action_pressed checks gives (1, 1), about 1.41 long, so diagonals are 41 % faster. Use Input.get_vector, or call .normalized() on your own vector.
  • Blurry pixels. Godot filters textures linearly by default. Set the sprite’s Texture > Filter to Nearest, or for the whole project Project Settings > Rendering > Textures > Canvas Textures > Default Texture Filter to Nearest.

Questions

How do I make 8-directional movement in Godot 4?

Read the input with Input.get_vector, which gives a vector no longer than 1 even on the diagonals, multiply it by a speed, set velocity and call move_and_slide on a CharacterBody2D. The full script is above.

Do I need eight sides, or can I mirror some?

Five are enough if the character looks the same mirrored: South, Southeast, East, Northeast and North, with flip_h for the west sides. Render all eight when the character holds something in one hand or is not symmetrical.

Why does my character face the wrong way going up or down?

Godot's 2D y axis points down, so a positive y velocity is moving down the screen, towards South. If up and down are swapped, the sector table was written for a y-up axis.

Read next

Try it on your own model, free in your browser, or get the full version.

Try in browser Get Pixor