Jumping animations not working as intended

Godot Version

4.4.1

Question

Hello everyone, I’m new to Godot (and appart from some previous small experience with Python, to programming). I’m currently working on small projects to learn, this one is about creating a player template suited for 2D platformers.

The issue is that all my animations (walking_right, walking_left, iddle_right, iddle_left) are working as intended except for the jumping ones (jumping_right, jumping_left), which are not showing up. I’ve used the script offered by the CharacterBody2D template and I’ve built the rest from there mainly using trial and error, but I haven’t been able to solve this last problem. That’s the code, any help will be highly appreciated:

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

var previous_direction = 1

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.
var direction := Input.get_axis("walk_left", "walk_right")
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

#Handle animation
if Input.is_action_pressed("walk_right") and is_on_floor():
	$AnimatedSprite2D.play("walking_right")
	previous_direction = 1
elif Input.is_action_pressed("walk_left") and is_on_floor():
	$AnimatedSprite2D.play("walking_left")
	previous_direction = -1

if velocity.x == 0 and previous_direction == 1:
	$AnimatedSprite2D.play("iddle_right")
elif velocity.x == 0 and previous_direction == -1:
	$AnimatedSprite2D.play("iddle_left")

if not is_on_floor() and previous_direction == 1:
	$AnimatedSprite2D.play("jumping_right")
elif not is_on_floor() and previous_direction == -1:
	$AnimatedSprite2D.play("jumping_left")

move_and_slide()

Hi,

That’s just a guess, but maybe the conditions for the idle animations will be valid even if you’re jumping, and velocity.x can be equal to 0 while jumping. In such a case, you will play an idle animation, and then a jump animation, which may be an issue, as you only want to play a jump animation.

Maybe add is_on_floor() conditions?

if is_on_floor() and velocity.x == 0 and previous_direction == 1:
	$AnimatedSprite2D.play("iddle_right")
elif is_on_floor() and velocity.x == 0 and previous_direction == -1:
	$AnimatedSprite2D.play("iddle_left")

if not is_on_floor() and previous_direction == 1:
	$AnimatedSprite2D.play("jumping_right")
elif not is_on_floor() and previous_direction == -1:
	$AnimatedSprite2D.play("jumping_left")

Also, for code readability purpose, know that you can group your if statements like this:

if is_on_floor() and and velocity.x == 0:
    if previous_direction == 1:
        $AnimatedSprite2D.play("iddle_right")
    else:
        $AnimatedSprite2D.play("iddle_left")
1 Like

Hello,
Thank you for your answer. I was so focused on fixing the jumping animation that I didn’t even thought the issue could be elsewere. This resolved the issue! Also, I’m taking good note of the readability advice.