8 way movement animations

Godot Version

Godot 4.3

Question

I am very new to Godot and coding in general. Ive got an 8-way movement system and I’ve tried multiple ways to add animations but I can’t figure it out, I’ve watch multiple videos and tried chat gpt but gotten no results.

extends CharacterBody2D

@export var speed = 150

func get_input():
var input_direction = Input.get_vector(“left”, “right”, “up”, “down”)
velocity = input_direction * speed

func _physics_process(delta):
get_input()
move_and_slide()

any help to add the animations would be great!
Thankyou :slight_smile:

input_direction is of type Vector2 and holds information about which direction your character is moving in. There are two such directions: x and y. If x is zero, the character is standing still in the x direction (i.e. moving neither right nor left.) If x is positive, the character moves right. If x is negative, the character is moving left. It’s similar for the y direction.

All put together, the character can be in one of nine movement states (if we include standing still as a state.) You could use something like a match statement to figure out which one applies, given input_direction as input:

match input_direction:
	case Vector2(0, 0):
		# standing still
	case Vector2(-1, 0):
		# moving left
# et cetera

Add an AnimationPlayer node as a child to your character scene, add some animations, and give each animation a name. For instance, character_idle. You can then change the animation from each of the match statement’s cases:

match input_direction:
	case Vector2(0, 0):
		$AnimationPlayer.play("character_idle")
	case Vector2(-1, 0):
		$AnimationPlayer.play("character_left")
# et cetera

edited to add: you probably don’t want to use _physics_process to process button input. _process is probably a better place for that. (_physics_process depends on your physics ticks; _process is called once per frame.)

1 Like

Look into AnimationTree with BlendSpace2D (I think it’s called) :slight_smile: