Crouch animation not playing

Hello! Brand new to game development and have been using Godot to learn my way around.

I have my player movements set up and am now working on adding the animations. the idle, run, jump, and double jump animations have all come out how I want them, but I am now trying to add a crouching idle animation. Whenever I press the crouch input, it switches to the first frame of the animation but doesn’t move from there.

Have tried both enabling and disabling looping in the SpriteFrames to no avail

Any advice would be much appreciated. Assuming I can figure this out, will eventually be wanting to add a crouched movement animation.

Thanks so much!

Video of problem

My (likely very jank) script:

extends CharacterBody2D


var SPEED = 300.0
const JUMP_VELOCITY = -400.0

# the jump count
var jump_count = 0
var max_jumps = 2

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

@onready var animated_sprite = $AnimatedSprite2D

func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta
		
	if is_on_floor():
		jump_count = 0

	# Handle jump.
	if Input.is_action_just_pressed("jump") and jump_count < max_jumps:
		velocity.y = JUMP_VELOCITY
		jump_count += 1

	# Handle Crouch.
	if Input.is_action_pressed("crouch"):
		$CrouchShape.disabled = false
		$DefaultShape.disabled = true
		
		SPEED = 200
		max_jumps = 0
	
	else:
		$CrouchShape.disabled = true
		$DefaultShape.disabled = false
		
		SPEED = 300
		max_jumps = 2


	# Get the input direction: -1, 0, 1
	var direction = Input.get_axis("move_left", "move_right")
	
	# Flip the Sprite
	if direction > 0:
		animated_sprite.flip_h = false
	elif direction < 0:
		animated_sprite.flip_h = true
	
	# play Animations
	if is_on_floor():
		if direction == 0:
			animated_sprite.play("Idle")
		else:
			animated_sprite.play("Run")
		
	else:
		if jump_count == 1:
			animated_sprite.play("Jump")
	if jump_count == 2:
		animated_sprite.play("Double Jump")
		
	if Input.is_action_pressed("crouch") and is_on_floor():
		animated_sprite.play("Crouch Idle")
	
	# Apply movement
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()
1 Like

The animation gets overwritten by the “Idle”-Animation. Heres a fix:

	# play Animations
	if is_on_floor():
        if Input.is_action_pressed("crouch"):
		    animated_sprite.play("Crouch Idle")
        else:
		    if direction == 0:
			    animated_sprite.play("Idle")
		    else:
			    animated_sprite.play("Run")
		
	else:
		if jump_count == 1:
			animated_sprite.play("Jump")
	if jump_count == 2:
		animated_sprite.play("Double Jump")

I think you can really benefit from implementing a “State-Machine”. There are a lot of tutorials on youtube. They are very useful and help you structure your animations and code

That worked! Thank you so much for taking the time. What you put down makes lot of sense

and yes, totally agreed. I started looking at getting a state-machine put together and was getting a little overwhelmed. Familiarizing myself with that will be my next goal.

1 Like

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