The player walk animation is still playing after going in the idle state

Godot Version

4.2.2.stable

Question

I’ve tested the animations* and its not working. lets say you walk in a specific direction, and you stop. but tour legs are still moving.
how do i make an animation play as long as a curtain button is held?
*i have a different animation for each direction. ask for my code if you need it.

Code would be helpful here

extends CharacterBody2D

const SPEED = 35.0

@onready var animated_sprite = $AnimatedSprite2D
@onready var running = $“…”

func _physics_process(_delta):
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis(“Left”, “Right”)
var direction2 = Input.get_axis(“Up”, “Down”)
velocity = velocity.normalized()

# Movement
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
if direction2:
	velocity.y = direction2 * SPEED
else:
	velocity.y = move_toward(velocity.y, 0, SPEED)

	
# Animation
if Input.is_action_just_pressed("Up"):
	animated_sprite.play("walk back")
if Input.is_action_just_released("Up"):
	animated_sprite.play("idle back")
if Input.is_action_just_pressed("Down"):
	animated_sprite.play("walk front")
if Input.is_action_just_released("Down"):
	animated_sprite.play("idle front")
if Input.is_action_just_pressed("Right"):
	animated_sprite.play("walk right")
if Input.is_action_just_released("Right"):
	animated_sprite.play("idle right")
if Input.is_action_just_pressed("Left"):
	animated_sprite.play("walk left")
if Input.is_action_just_released("Left"):
	animated_sprite.play("idle left")

move_and_slide()

I messed around with the code a little after but still need revised for a more accurate way for the character to move with the correct animations

I’d suggest moving the player’s walk function into its own function. It also looks like, because you’re checking immediately after the initial “is_action_just_pressed” with an “is_action_just_released”; ideally these should be in separate if/elif statements.

Here’s an example based off the player I currently have open and working on:

func do_player_input() -> void:
	# Basic left/right
	var direction = Input.get_axis("Left", "Right")
	if direction:
		velocity.x = direction * speed
		# Moves the player/animation left/right
		if Input.is_action_pressed("Left") and is_crouching == false:
			position2D.scale.x = -1
			anim.play("chesterrun")
		elif Input.is_action_pressed("Right") and is_crouching == false:
			position2D.scale.x = 1
			anim.play("chesterrun")
	else:
		velocity.x = move_toward(velocity.x, 0, speed)
		# Decides which direction idle animation plays
		if Input.is_action_just_released("Right") and is_crouching == false:
			position2D.scale.x = 1
			anim.play("chesteridle")
		elif Input.is_action_just_released("Left") and is_crouching == false:
			position2D.scale.x = -1
			anim.play("chesteridle")
		elif Input.is_action_just_released("Crouch"):
			anim.play("chesteridle")

position2D is a Marker 2D I have placed to flip sprites left/right; but the key takeaway is all of the left/right/up/down should be in one ‘if’ block, and the released left/right/etc is in an ‘elif/else’ block.

EDIT: anim is another variable that is related to an AnimatedSprite2D; you can ignore that if it’s not relevant.

EDIT2: if you do move the player’s input into its own function, make sure to include the ‘do_player_input()’ function in your physics_process function, or it won’t work. It just neatens up the code in the physics_process function.

please dumb this down because i am new to… Godot and its scripting language or any language at that. plus im making a top down game where the player wont jump and will need to have an idle plus walking animation for up , down, left, and right. of course you know this by analyzing the code.

So, if you are wanting to switch to an idle animation whenever the player is finished walking, one of the ways you can achieve it is by putting two separate blocks within the movement code.

Instead of doing:

if Input.is_action_just_pressed("Up"):
	animated_sprite.play("walk back")
if Input.is_action_just_released("Up"):
	animated_sprite.play("idle back")
if Input.is_action_just_pressed("Down"):
	animated_sprite.play("walk front")
if Input.is_action_just_released("Down"):
	animated_sprite.play("idle front")
if Input.is_action_just_pressed("Right"):
	animated_sprite.play("walk right")
if Input.is_action_just_released("Right"):
	animated_sprite.play("idle right")
if Input.is_action_just_pressed("Left"):
	animated_sprite.play("walk left")
if Input.is_action_just_released("Left"):
	animated_sprite.play("idle left")

You’d want to do something like:

if direction:
     if Input.is_action_just_pressed("Up"):
          animation_sprite.play("walk back") 
     elif Input.is_action_just_pressed("Left"):
          animation_sprite.play("walk left"):
     elif Input.is_action_just_pressed("Down"):
          animation_sprite.play("walk front")
     elif Input.is_action_just_pressed("Right"):
          animation_sprite.play("walk right")
else:
     if Input.is_action_just_released("Up"):
          animation_sprite.play("idle back") 
     elif Input.is_action_just_released("Left"):
          animation_sprite.play("idle left"):
     elif Input.is_action_just_released("Down"):
          animation_sprite.play("idle front")
     elif Input.is_action_just_released("Right"):
          animation_sprite.play("idle right")