i'm currently trying to make a small platformer game to learn since i'm relatively new to coding and such but i'm having issues with the jump animation.

at first the animation wouldn't even play, the character continuously playing the idle animation in midair. now i managed to get them to actually play the jump animation but it's stuck on the first frame. i've looked at pass qnas and tried the answers from those qnas but it still won't work.

here's my code:

``````extends KinematicBody2D

var velocity = Vector2(0,0)

const SPEED = 150
const GRAV = 10
const JUMPFORCE = -400

func _physics_process(delta):
if Input.is_action_pressed("right"):
velocity.x = SPEED
\$Sprite.play("walk")
\$Sprite.flip_h = false
elif Input.is_action_pressed("left"):
velocity.x = -SPEED
\$Sprite.play("walk")
\$Sprite.flip_h = true
else:
\$Sprite.play("idle")

if not is_on_floor():
\$Sprite.play("air")

if Input.is_action_just_pressed("up"):
velocity.y = JUMPFORCE

#gravity
velocity.y = velocity.y + GRAV

#actually getting them to move
velocity = move_and_slide(velocity, Vector2.UP)

velocity.x = lerp(velocity.x, 0, 0.1)

if velocity.y == 0:
velocity.y = 10
``````
in Engine

+1 vote

The problem is that you changing playing animation during jump twice per frame. First you check input and based on speed play walk or idle animation. Then you play air animation if player is in air. Because you changed your animation to walk or idle, air animation will start from the beginning. To solve this problem you need to check if player is in air, and if it is true, then do not change animation to walk or idle.

by (1,644 points)

You can add flag variable `var should_change_animation = true` to indicate that there was no animations change. Then when player is air you change animation and set this variable to `false`. And after that every time you want to change animation you make next check:

``````if should_change_animation:
should_change_animation = false
\$Sprite.play("idle")
``````

How to indicate var should changeanimation = true

you can save last animation name and then check if current animation name was changed. example in this case would look like

``````var last_animation_name := ""
func _physics_process(_delta: float) -> void:
var current_animation_name := "idle"
if move_left:
current_animation_name = "move_left"
elif move_right:
current_animation_name = "move_right"
elif jump:
current_animation_name = "jump"

if last_animation_name != current_animation_name:
\$animation_player.play(current_animation_name)
last_animation_name = current_animation_name
``````