Using AnimatedSprite2D. I coded animations to play when you're pressing arrows and stop playing (also setting the frame to the first one) when player stands still. However there's a tiny issue when going diagonally. There is inconsistency with x and y axis: on x it changes the animation to the new direction and on y it ignores it.
I want it to check if you're already walking in a direction, and if that's true not change the animation to another direction... something like in Undertale. The problem is I don't really know how to implement that properly. I'd appreciate some suggestions. Thanks in advance!
A question on 2d 8-directional animation
- Edited
- Best Answerset by Megalomaniak
Hey guys for anyone else wondering I've figured it out, here's what it looks like. Not the best but at least it works :)
extends CharacterBody2D
@export var speed = 140
func _process(_delta):
walking()
func walking():
#movement
var direction = Vector2()
#animation
var fuck = $animation
#walking is possible thanks to this
if Input.is_action_pressed("ui_right"):
direction.x += 1
if Input.is_action_pressed("ui_left"):
direction.x -= 1
if Input.is_action_pressed("ui_down"):
direction.y += 1
if Input.is_action_pressed("ui_up"):
direction.y -= 1
#animation
match direction:
Vector2(1, 0):
fuck.play()
fuck.set_animation("right")
Vector2(-1, 0):
fuck.play()
fuck.set_animation("left")
Vector2(0, 1):
fuck.play()
fuck.set_animation("down")
Vector2(0, -1):
fuck.play()
fuck.set_animation("up")
# more movement junk.
# make sure it's placed after the match statement.
direction = direction.normalized()
velocity = direction * speed
move_and_slide()
#animation stops
if velocity == Vector2.ZERO:
fuck.stop()