Need help with animation switchups

Godot 4.3

Hello! I’m trying to make my animations switch up when certain things happen, so when you press left or right, the idle animated sprite 2d disappears and the walking animated sprite 2d. But certain animations won’t go away (The reason for the different animated sprite 2d’s is because of the different canvas sizes for all of them). For example the walking and idle animation don’t go away when the run button is pressed. Can someone help?

BTW The animated sprite 2d “Animations” is for the idle, just letting you guys know.

	if Input.is_action_just_pressed("left"):
		if Input.is_action_just_pressed("run") == false:
			$Walk.show()
			$Walk.play("walk")
			$Animations.hide()
	if Input.is_action_just_pressed("right"):
		if Input.is_action_just_pressed("run") == false:
			$Walk.show()
			$Walk.play("walk")
			$Animations.hide()
	if Input.is_action_just_pressed("left"):
		if Input.is_action_just_pressed("run") == true:
			$Walk.hide()
	if Input.is_action_just_pressed("right"):
		if Input.is_action_just_pressed("run") == true:
			$Walk.hide()
	if Input.is_action_just_released("left"):
		$Walk.hide()
		$Animations.show()
	if Input.is_action_just_released("right"):
		$Walk.hide()
		$Animations.show()

	if Input.is_action_just_pressed("run"):
		SPEED = 670
		$Run.show()
		$Run.play("Run")
		$Animations.hide()
		$Walk.hide()
	if Input.is_action_just_released("run"):
		SPEED = 450
		$Run.stop()
		$Run.hide()
	var direction := Input.get_axis("left", "right")
		if direction:
		velocity.x = direction * SPEED
		$Walk.play("walk")
	else:
		velocity.x = move_toward(velocity.x, 0, 22)

Are you using 3 different AnimatedSprite2D?

You should put them all into one:

Just click “New” in the top left corner.
In that case you can automatically overwrite them by calling .play()

@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(delta):
    # Determine movement state
    var is_running = Input.is_action_pressed("run")
    var direction := Input.get_axis("left", "right")
    var is_moving = direction != 0
    
    # Set speed based on running state
    if is_running:
        SPEED = 670
    else:
        SPEED = 450
    
    # Handle movement
    if is_moving:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, 22)
    
    # Handle animations
    if is_moving:
        if is_running:
            animated_sprite.play("run")
        else:
            animated_sprite.play("walk")
    else:
        animated_sprite.play("idle")

If you also want to flip your Sprite based on direction:

    # Flip sprite based on direction
    if direction < 0:
        animated_sprite.flip_h = true
    elif direction > 0:
        animated_sprite.flip_h = false
1 Like

well all the animations are different canvas sizes so that’s why I put them all in separate animated sprite 2ds

Okay, in that case I would change the sizes to fit each other, so you can use only one AnimatedSprite2D.

It’s better for your workflow, when you have to change things in the future.
The script also becomes cleaner and easier to understand.