AnimatedSprite2D not playing a particular animation when shooting

Godot Version

v4.2.2

Question

I’m working on a shooter project and working on my player scene to move and shoot. I was able to add 4 different animations on the AnimatedSprite2D for walking forward, up, down and shoot.
I was able to animate when walking forward, up and down but the shoot animation is not playing.

Here is my Script:

extends CharacterBody2D

var direction: Vector2 = Vector2.ZERO
@export var speed: int = 350
var bullet_scene = preload(“res://bullet.tscn”)
signal bullet_shot(bullet_scene, location)
@onready var gun = $Gun
var shoot_cd = false

INPUTS

func _process(_delta):
direction = Input.get_vector(“move_left”,“move_right”,“move_up”,“move_down”)
update_animation(direction)
if Input.is_action_pressed(“shoot”):
if !shoot_cd:
shoot_cd = true
shoot()
await get_tree().create_timer(0.90).timeout
shoot_cd = false

SHOOT

func shoot():
$AnimatedSprite2D.play(“shoot”)
bullet_shot.emit(bullet_scene, gun.global_position)

ANIMATION

func update_animation(direction: Vector2):
if direction.y < 0:
$AnimatedSprite2D.play(“walk_up”)
elif direction.y > 0:
$AnimatedSprite2D.play(“walk_down”)
else:
$AnimatedSprite2D.play(“walk”)

PHYSICS

func _physics_process(_delta):
velocity = direction * speed
move_and_slide()

LIMITS

global_position.x = clamp(global_position.x, 45,1090)
global_position.y = clamp(global_position.y, 80,500)

I tought that the animation would play once the func shoot() was activated but it is not playing. Everything works as far as I know but the “shoot” animation doesn’t play.

I have already tested the animation running the particular scene and also tested this way:

@onready var animated_sprite = $AnimatedSprite2D

func _ready():
print(“Ready function called”)
animated_sprite.play(“shoot”)
await get_tree().create_timer(1.0).timeout # Tempo suficiente para ver a animação
print(“Shoot animation test complete”)
if animated_sprite.is_playing():
print(“Animation is playing”)
else:
print(“Animation is not playing”)

I get the print (“Animation is playing”) but the animation is not actually played.
Here is a video of what is happening: https://youtu.be/WAbBN2EEaLM

define a varaible of shooting = false and then do like this:

func update_animation(direction: Vector2):
  if shooting: return
    if direction.y < 0:
      $AnimatedSprite2D.play(“walk_up”)
    elif direction.y > 0:
      $AnimatedSprite2D.play(“walk_down”)
    else:
      $AnimatedSprite2D.play(“walk”)

And

func shoot():
  $AnimatedSprite2D.play(“shoot”)
  bullet_shot.emit(bullet_scene, gun.global_position)
  shooting = true

Do not forgot to make the shooting to false, you can use a timer for it

1 Like