How to code a non-intrusive idle animtion

Godot Version

v4.5.1.stable.official

Question

I've been trying to make a crouching animation on my player script but it only plays the first frame of animation, wich i realized is because my idle animation is interfering. I’m new to coding and haven’t been able to find a suitable workaround.

@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(delta: float) -> void:
	
	# Get the input direction and handle the movement/deceleration.
	var direction := Input.get_axis("walk_back", "walk_forward")
	var crouch := Input.get_action_raw_strength("crouch") and is_on_floor()
	
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
		animated_sprite.play("jump")
	
	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY
	
	#Handle movement.
	if direction and is_on_floor():
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	
	#Walking animations here.
	if is_on_floor():
		if direction == 0 :
			animated_sprite.play("Idle")
		if direction > 0:
			animated_sprite.play("walk forward")
		if direction < 0:
			animated_sprite.play("walk back")
	
	#How do i make you crouch? 
	if crouch:
		animated_sprite.play("crouch")
		print("im crouch'd trust")
	
	
	move_and_slide()

Don’t play idle if crouch is playing.

I would add a line of code for the Idle animation like if direction == 0 and player != crouching:

animated_sprite.play(“idle”) so Idle only plays when player isnt crouching. Im also new so dont count on it working but I would say that fixes the problem if you have a animation for direction beeing 0 and the player beeing in crouch. If you have the same problem with crouching for all directions you could probably add “and is not crouching to all the not crouching if conditions. Hope this helps :slight_smile:

or you could make a condition for all the „standing“ animation, for it to only work when the crouching button is not pressed

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.