Why jump animation is not playing?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By randomguy
extends KinematicBody2D

const GRAVITY = 50
const JUMP_FORCE = -1100
const SPEED = 200
const UP = Vector2.UP

var velocity = Vector2()
var facing_left = false
var facing_right = true

func _physics_process(delta):
velocity.y += GRAVITY

if Input.is_action_pressed("move_left"):
	facing_left = true
	facing_right = false
	velocity.x = -SPEED
	$AnimationPlayer.play("run_left")
elif Input.is_action_pressed("move_right"):
	facing_left = false
	facing_right = true
	velocity.x = SPEED
	$AnimationPlayer.play("run_right")
else:
	velocity.x = 0
	if facing_left:
		$AnimationPlayer.play("idle_left")
	elif facing_right:
		$AnimationPlayer.play("idle_right")

if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = JUMP_FORCE
	if facing_left:
		$AnimationPlayer.play("jump_left")
	elif facing_right:
		$AnimationPlayer.play("jump_right")

velocity = move_and_slide(velocity, UP)

Try adding some debug print()'s after each animation play just sp you know if it’s even being called or not.

SF123 | 2022-05-05 03:10

It is being called, but the animation is not playing, it looks like it appears for only a split second and then immediately changes to running or idle animation.

randomguy | 2022-05-05 08:37

:bust_in_silhouette: Reply From: Mario

This seems to be a classic beginner mistake:

Your jump animation should start properly. However, the very next frame the first section of your movement code will most likely get into its else portion, where you’ll set idle_left or idle_right, overwriting your jump.

You’ll need an extra boolean variable (or some other variable representing your character’s current state) so your movement code doesn’t set a move or idle animation while you’re jumping.