Double jump animation does not work

Godot Version

4.1.3.stable

Question

Hi IndieQuantum here,

I need some help with my double jump animation not playing. All of my players animations are in a single AnimatedSprite2D node.

My jump animation (a single frame) works when jumping, but when double jumping (which works) the animation for double jumping (which is 6 frames) does not play. It only plays it for a split second but then gets taken over by the jump animation.

I don’t now how to code this to get it to work, please help!

Here is the script for my player(I apologize for the messy script, beginner here):
extends CharacterBody2D

var jump_force = -160
var jump_release_force = -40
var max_speed = 75
var acceleration = 10
var friction = 10
var gravity = 5
var additional_fall_gravity = 2
var double_jump = 1
var double_jump_count = 1

func _physics_process(_delta):
apply_gravity()
var input = Vector2.ZERO
input.x = Input.get_action_strength(“move_right”) - Input.get_action_strength(“move_left”)

if input.x == 0:
	apply_friction()
	$AnimatedSprite2D.play("idle")
else:
	apply_acceleration(input.x)
	$AnimatedSprite2D.play("run")
	if input.x > 0:
		$AnimatedSprite2D.flip_h = false
	elif input.x < 0:
		$AnimatedSprite2D.flip_h = true

if is_on_floor():
	double_jump = double_jump_count
	if Input.is_action_just_pressed("jump"):
		velocity.y = jump_force
else:
	if Input.is_action_just_released("jump") and velocity.y < jump_release_force:
		velocity.y = jump_release_force
	if Input.is_action_just_pressed("double_jump") and double_jump > 0:
		velocity.y = jump_force
		double_jump -= 1
		$AnimatedSprite2D.play("double_jump")
	if velocity.y > 0:
		velocity.y += additional_fall_gravity
		$AnimatedSprite2D.play("fall")
	elif velocity.y < 0:
		$AnimatedSprite2D.play("jump")
	
var was_in_air = not is_on_floor()
move_and_slide()
var just_landed = is_on_floor() and was_in_air
if just_landed:
	$AnimatedSprite2D.play("run")
	$AnimatedSprite2D.frame = 1

func apply_gravity():
velocity.y += gravity

func apply_friction():
velocity.x = move_toward(velocity.x,0,friction)

func apply_acceleration(amount):
velocity.x = move_toward(velocity.x,max_speed * amount,acceleration)

you can check the value of the double jump variable before playing the jump animation and choose to play the double jump animation if it’s 0

Thank you for the reply. I am sorry but I don’t quite understand.

	elif velocity.y < 0:
                 if double_jump > 0:
       		$AnimatedSprite2D.play("jump")
                 else:
       		$AnimatedSprite2D.play("double_jump")

Don’t copy and paste this, i typed it in the forum editor so the indentation is all messed up

1 Like

This work now! the only problem is that the double jump animation only plays one frame and that’s it. not the whole animation.

did you indent it properly? it looked fine when i typed it in the editor but the actual post has the indentation all messed up

Probably an issue comming from when you setup the animatioon in the animation editor

yes I indented correctly, i did not copy and paste

in my animatedsprite2d node, I have several animations in it. idle, run, jump, and fall animation works just fine. here is a screen shot:

one reason that the animation only plays the first frame is that it is also playing another animation before playing the double jump animation each frame.

for example you will play the idle or run animation before playing any fall or jump animation so the jump animation will reset to the first frame

you can fix this by putting the animation logic all in one place, and checking if the character is in the air before playing idle/run animation

1 Like

another way I could solve this is to have another animatedsprite2d node with only the double jump animation in it, and then have it play when ever I double jump.

then you would have to hide the other animatedsprite2d node when the player double jumps, it would be the same logic, just more nodes

1 Like

I just used a second animatedsprite2d that only has the double jump animation. I used the logic you provided to play around with the visibility of the two animated sprite nodes. So thank you for all your help!