Having trouble with jump animations

Godot Version

Godot version 4

So its my first time using godot apart from the tutorial and I am having trouble with the jump animation in my game which is not working properly like it plays fine the first time but after that instead of jump animation the player just keeps doing the run animation if I am running and jumping`and idle animation if I am idle.

extends CharacterBody2D

@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D

const SPEED = 150.0
const JUMP_VELOCITY = -500.0
var Facing_Direction : String = "R"
var animation_played: bool = false



func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
	
	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY
	
	# 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("move_left", "move_right")
	
	if Input.is_action_pressed("move_left"):
		Facing_Direction = "L"
	if Input.is_action_pressed("move_right"):
		Facing_Direction = "R"
	
	#Sprite Direction
	if is_on_floor():
		if direction == 0:
			animated_sprite.play("Idle." + Facing_Direction)
			#print("Idle." + Facing_Direction)
		else:
			animated_sprite.play("Run." + Facing_Direction)
			#print("Run." + Facing_Direction)
	elif velocity.y > 0:
		velocity = get_gravity() * delta * 5
		animated_sprite.play("Fall." + Facing_Direction)
		#print("Fall." + Facing_Direction)
	if Input.is_action_pressed("jump"):
		if not animation_played: # Only play if not already played
			animated_sprite.play("Jump." + Facing_Direction)
			print("Is Jumping")
			animation_played = true
	
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

func _on_animation_finished(animation_name):
	if animation_name == "Jump.R" or animation_name == "Jump.L":
		animation_played = false # Reset the flag when finished

Is the _on_animation_finished signal wired correctly? If you’ve connected it through the editor, it breaks when you move around nodes or resources.

It makes sense if it works the first time, but not after that if the function is never called → animation_played is never reset.

EDIT:
You can double check if it’s wired if there’s a green arrow next to the function in the editor.

1 Like

Yep, I connected it through the editor and there is no green arrow, so is there a way I can connect it properly

You can just do it again through the editor. It won’t override your code or anything.

Or you can wire it through code using the .connect method on your animated sprite. People generally prefer this because it doesn’t break as easily, and when it does it throws an error or warning (something like “signal is defined but never called in script”).

Will try this and let you know, thanks for the assist.

Tried it and still nothing. I guess the problem is that the “animation_played” flag is not resetting to false. If you can help with it, it would be great.

Edit: Didn’t use .connect since I don’t know how to use it

You can debug by printing something in the function to see if it fires at all. If it doesn’t you didn’t wire the signal correctly.

The connect syntax is pretty straightforward if you follow the documentation. You connect it in the code of the parent node that you want to receive the signal (player in your case). Write this in the ready function. Pseudocode:

NodeThatEmitsSignal.signalname.connect(function_thats_called_on_signal)

It’s the same logic as connecting through the editor, but this one relies on your node reference, which is harder to break, especially if you @export it.