Animation not playing?

I followed a tutorial that did not let my play move for some reason, but with help from the comments the movement works fine, but movement animations will not play. Help?

extends CharacterBody2D

const speed = 100
var current_dir = "none"

func _ready():
	$AnimatedSprite2D.play("front_idle")

 

var direction : Vector2 = Vector2.ZERO

func _physics_process(delta):
	
	
	direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down").normalized()
	if direction:
		velocity = direction * speed
	else:
		velocity = Vector2.ZERO
	

	move_and_slide()
func play_anim(movement):
	var dir = current_dir
	
	if dir == "right":
		$AnimatedSprite2d.flip_h = false
		if movement == 1:
			$AnimatedSprite2d.play("side_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("side_idle")
			
	if dir == "left":
		$AnimatedSprite2d.flip_h = true
		if movement == 1:
			$AnimatedSprite2d.play("side_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("side_idle")

	if dir == "up":
		$AnimatedSprite2d.flip_h = false
		if movement == 1:
			$AnimatedSprite2d.play("back_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("back_idle")

	if dir == "down":
		$AnimatedSprite2d.flip_h = false
		if movement == 1:
			$AnimatedSprite2d.play("front_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("front_idle")

Hi!

It seems you’re not calling the play_anim function at all, right?
Which would explain why no animation is playing.

I suppose you could do something like this:

if direction:
    velocity = direction * speed
    play_anim(1)
else:
    velocity = Vector2.ZERO
    play_anim(0)

Also, even if you call the function, it’s not going to do anything. dir is always “none” according to your code.

1 Like

you didnt add " play_anim(movement) " in your physics_process function, that’s why. the code will be ignored because the function is not getting called so do this

extends CharacterBody2D

const speed = 100
var current_dir = "none"

func _ready():
	$AnimatedSprite2D.play("front_idle")

 

var direction : Vector2 = Vector2.ZERO

func _physics_process(delta):


	play_anim(movement) # calls the function
	
	direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down").normalized()
	if direction:
		velocity = direction * speed
	else:
		velocity = Vector2.ZERO
	

	move_and_slide()
func play_anim(movement):
	var dir = current_dir
	
	if dir == "right":
		$AnimatedSprite2d.flip_h = false
		if movement == 1:
			$AnimatedSprite2d.play("side_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("side_idle")
			
	if dir == "left":
		$AnimatedSprite2d.flip_h = true
		if movement == 1:
			$AnimatedSprite2d.play("side_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("side_idle")

	if dir == "up":
		$AnimatedSprite2d.flip_h = false
		if movement == 1:
			$AnimatedSprite2d.play("back_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("back_idle")

	if dir == "down":
		$AnimatedSprite2d.flip_h = false
		if movement == 1:
			$AnimatedSprite2d.play("front_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("front_idle")

but i recommend putting that line of code in the process function because if you put it on physics process, you can get a little bit of performance loss:

extends CharacterBody2D

const speed = 100
var current_dir = "none"

func _ready():
	$AnimatedSprite2D.play("front_idle")

 

var direction : Vector2 = Vector2.ZERO

func _process(delta: float) -> void:
   play_anim(movement)

func _physics_process(delta):
	
	
	direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down").normalized()
	if direction:
		velocity = direction * speed
	else:
		velocity = Vector2.ZERO
	

	move_and_slide()
func play_anim(movement):
	var dir = current_dir
	
	if dir == "right":
		$AnimatedSprite2d.flip_h = false
		if movement == 1:
			$AnimatedSprite2d.play("side_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("side_idle")
			
	if dir == "left":
		$AnimatedSprite2d.flip_h = true
		if movement == 1:
			$AnimatedSprite2d.play("side_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("side_idle")

	if dir == "up":
		$AnimatedSprite2d.flip_h = false
		if movement == 1:
			$AnimatedSprite2d.play("back_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("back_idle")

	if dir == "down":
		$AnimatedSprite2d.flip_h = false
		if movement == 1:
			$AnimatedSprite2d.play("front_walk")
		elif movement == 0:
			$AnimatedSprite2d.play("front_idle")