Initially, when I move diagonally my sprite only looks to the side but I want it to look up and down when I move diagonally. I tried adding this code under the left and right movement animation.
if Input.is_action_pressed("up_key"):
anim.play("back_walk")
elif Input.is_action_pressed("down_key"):
anim.play("front_walk")
It now looks up and down but the animation doesn’t play. How can I properly do this?
Script:
extends CharacterBody2D
@export var speed = 130
var current_dir = "none"
func get_input():
var input_direction = Input.get_vector("left_key", "right_key", "up_key", "down_key")
velocity = input_direction * speed
func _physics_process(delta):
get_input()
move_and_slide()
var anim = $AnimatedSprite2D
if Input.is_action_pressed("left_key"):
current_dir = "left"
play_anim(1)
elif Input.is_action_pressed("right_key"):
current_dir = "right"
play_anim(1)
elif Input.is_action_pressed("up_key"):
current_dir = "up"
play_anim(1)
elif Input.is_action_pressed("down_key"):
current_dir = "down"
play_anim(1)
else:
play_anim(0)
func play_anim(movement):
var dir = current_dir
var anim = $AnimatedSprite2D
if dir == "left":
anim.flip_h = true
if movement == 1:
anim.play("side_walk")
if Input.is_action_pressed("up_key"):
anim.play("back_walk")
elif Input.is_action_pressed("down_key"):
anim.play("front_walk")
elif movement == 0:
anim.play("side_idle")
if dir == "right":
anim.flip_h = false
if movement == 1:
anim.play("side_walk")
if Input.is_action_pressed("up_key"):
anim.play("back_walk")
elif Input.is_action_pressed("down_key"):
anim.play("front_walk")
elif movement == 0:
anim.play("side_idle")
if dir == "up":
if movement == 1:
anim.play("back_walk")
elif movement == 0:
anim.play("back_idle")
if dir == "down":
if movement == 1:
anim.play("front_walk")
elif movement == 0:
anim.play("front_idle")