I got some basic code for a crouch action that goes like this:


if is_on_floor() and Input.is_action_pressed("crouch"):
		crouching = true;
		$AnimationPlayer.play("Crouch");
	else:
		crouching = false;

For some reason though, my crouching animation goes to the first frame but won't play. I've double-checked the animation in the AnimationPlayer node and everything seems to be set up right, so I'm not sure what I did wrong.

Are you overriding the animation anywhere by calling $AnimationPlayer.play before playing the crouch animation? Like an idle animation or something similar? It is probably something is overriding the animation, so it keeps switching back and forth between the overriding animation and the crouch animation, which makes the crouch animation stuck on just the first frame.

@TwistedTwigleg said: Are you overriding the animation anywhere by calling $AnimationPlayer.play before playing the crouch animation? Like an idle animation or something similar? It is probably something is overriding the animation, so it keeps switching back and forth between the overriding animation and the crouch animation, which makes the crouch animation stuck on just the first frame.

I don't think so? I mean, the character visibly crouches, so it seems like it's transitioning to it, it's just not playing. Here's the full code in case that might help:

extends KinematicBody2D

export var walkSpeed : int;
export var runSpeed : int;
export var jumpSpeed : int;
export var gravity : int;
export var maxFallSpeed : int;

export var health : int;

var crouching = false;
var attacking = false;

var velocity : = Vector2();

func input():
	
#Movement
	if Input.is_action_pressed("right") and !crouching:
		$Sprite.flip_h = false;
		if Input.is_action_pressed("run"):
			velocity.x = runSpeed;
			$AnimationPlayer.play("Run");
		else:
			velocity.x = walkSpeed;
			$AnimationPlayer.play("Walk");
	elif Input.is_action_pressed("left") and !crouching:
		$Sprite.flip_h = true;
		if Input.is_action_pressed("run"):
			velocity.x = -runSpeed;
			$AnimationPlayer.play("Run");
		else:
			velocity.x = -walkSpeed;
			$AnimationPlayer.play("Walk");
	else:
		velocity.x = 0;
		$AnimationPlayer.play("IdleStand");

#Jump
	if is_on_floor() and Input.is_action_pressed("jump"):
		velocity.y = -jumpSpeed;
		$AnimationPlayer.play("Jump");

#Crouch
	if is_on_floor() and Input.is_action_pressed("crouch"):
		crouching = true;
		$AnimationPlayer.play("Crouch");
	else:
		crouching = false;

func _physics_process(delta):
	input();
	velocity.y += gravity * delta;
	if velocity.y > maxFallSpeed:
		velocity.y = maxFallSpeed;
	move_and_slide(velocity, Vector2.UP);

I think the IdleStand animation is causing the issue. Try this:

extends KinematicBody2D

export var walkSpeed : int;
export var runSpeed : int;
export var jumpSpeed : int;
export var gravity : int;
export var maxFallSpeed : int;

export var health : int;

var crouching = false;
var attacking = false;

var velocity : = Vector2();

func input():
	
#Movement
	if Input.is_action_pressed("right") and !crouching:
		$Sprite.flip_h = false;
		if Input.is_action_pressed("run"):
			velocity.x = runSpeed;
			$AnimationPlayer.play("Run");
		else:
			velocity.x = walkSpeed;
			$AnimationPlayer.play("Walk");
	elif Input.is_action_pressed("left") and !crouching:
		$Sprite.flip_h = true;
		if Input.is_action_pressed("run"):
			velocity.x = -runSpeed;
			$AnimationPlayer.play("Run");
		else:
			velocity.x = -walkSpeed;
			$AnimationPlayer.play("Walk");
	else:
		velocity.x = 0;
	# only play if not crouching
	if !crouching:
			$AnimationPlayer.play("IdleStand");

#Jump
	if is_on_floor() and Input.is_action_pressed("jump"):
		velocity.y = -jumpSpeed;
		$AnimationPlayer.play("Jump");

#Crouch
	if is_on_floor() and Input.is_action_pressed("crouch"):
		crouching = true;
		$AnimationPlayer.play("Crouch");
	else:
		crouching = false;

func _physics_process(delta):
	input();
	velocity.y += gravity * delta;
	if velocity.y > maxFallSpeed:
		velocity.y = maxFallSpeed;
	move_and_slide(velocity, Vector2.UP);

@TwistedTwigleg said: I think the IdleStand animation is causing the issue. Try this: Looks like that was the issue. Thanks for the help!

a year later