jump animation is stuck on first frame

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

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

#move please
func _physics_process(delta):
	if Input.is_action_pressed("right"):
		velocity.x = SPEED
		$Sprite.flip_h = false
	elif Input.is_action_pressed("left"):
		velocity.x = -SPEED
		$Sprite.flip_h = true
	if not is_on_floor():
	if Input.is_action_just_pressed("up"):
		velocity.y = JUMPFORCE
	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
:bust_in_silhouette: Reply From: AlexTheRegent

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.

can i ask how do i go about adding this?

kityeeeee | 2021-01-03 02:33

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

AlexTheRegent | 2021-01-07 13:42

How to indicate var should _change_animation = true

Madara_ghost | 2022-11-03 00:57

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:
	last_animation_name = current_animation_name

AlexTheRegent | 2022-11-09 14:56