Noob question, can't get jump animation to work

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

Hi, i’m a newcomer to coding and i have this problem with the character sprite animation, when i walk the animation works fine, when i do nothing the idle plays great, but when i jump the animation freeze on the first frame of the jump animation.
i searched the web and found some questions and answers about this issue, but i couldn’t implement there solutions to my code.
i hope some of you pro’s could help a newcomer out:)
BEST!

extends KinematicBody2D

var velocity = Vector2(0,0)
const SPEED = 295
const GRAVITY = 30
const JUMPFORCE = -780

func _physics_process(delta):

if Input.is_action_pressed("ui_right"):
	velocity.x = SPEED
	$Sprite.flip_h = false
	$Sprite.play("WALK")
elif Input.is_action_pressed("ui_left"):
	velocity.x = -SPEED
	$Sprite.flip_h = true
	$Sprite.play("WALK")
else :
	$Sprite.play("IDLE")

if not is_on_floor():
	$Sprite.play("JUMP")

velocity.y = velocity.y + GRAVITY
if Input.is_action_just_pressed("ui_up") and is_on_floor():
	velocity.y = JUMPFORCE

velocity = move_and_slide(velocity,Vector2.UP)
velocity.x = lerp(velocity.x,0,1)
:bust_in_silhouette: Reply From: Simon

Hi, try to use this in the function _process.

if velocity.y != 0:
    $Sprite.play("JUMP")

Hi,
thanks for the answer but when i do that the jump still doesn’t work and the walk cycle start to act strange.

https://streamable.com/u05dp2

shugishugi | 2020-07-27 06:23

Try with this:

if velocity.y != 0 and is_on_floor():
    $Sprite.play("JUMP")

Simon | 2020-07-27 17:23

:bust_in_silhouette: Reply From: ChristianSF

Have you checked that your jump animation is correctly set up, i.e. does it work properly in the Animations/Animations Frames section?
Maybe you just forgot to add the rest of the animation images or named the animation differently (not “JUMP”)? Hard to tell from here.

EDIT:
Just saw your example animation. Your code

if not is_on_floor():
    $Sprite.play("JUMP")

keeps restarting the jump animation several times per second, which is probably why you don’t see any other frames of it.

Hey,
For the first part of tour answer, yes the animation of the jump working in the animation editor
for the second part, how can i make it not to restart?

shugishugi | 2020-07-27 18:20

Well, one easy way, as I imagine, would be to add a variable jump_ani_playing, setting it to false as default value and only to true once the player char has left the ground (not hundreds of times each frame :wink: ).

Then change your existing code to:

var jump_ani_playing = false

... # The following is in your physicsprocess function:...

    if !is_on_floor() and !jump_ani_playing:
        $Sprite.play("JUMP")
        jump_ani_playing = true

So, when the player char lifts off for the first time, is_on_floor() returns false and jump_ani_playing is also still false, so the sprite will start playing its JUMP animation.

We set jump_ani_playing to true immediately afterwards, so that the next time the function runs, the IF statement as a whole will resolve to false and not restart the animation.

Should work (untested), but probably there are dozens of better ways to do that.

EDIT: For some reason, this Q&A system took the underscores in the variable names within my comment for “show this in italics”. Modified the comment using the code feature for clarity.

ChristianSF | 2020-07-27 19:27

hi ChristianSF,

thanks for the help, but still, not working.
what hapenning now is that when i jump it plays the walk animation or the idle animation (if i jump just up)

var jump_ani_playing = false

var velocity = Vector2(0,0)
const SPEED = 295
const GRAVITY = 30
const JUMPFORCE = -780

func _physics_process(delta):

if Input.is_action_pressed("ui_right"):
	velocity.x = SPEED
	$Sprite.flip_h = false
	$Sprite.play("WALK")
elif Input.is_action_pressed("ui_left"):
	velocity.x = -SPEED
	$Sprite.flip_h = true
	$Sprite.play("WALK")
else :
	$Sprite.play("IDLE")


velocity.y = velocity.y + GRAVITY
if Input.is_action_just_pressed("ui_up") and is_on_floor():
	velocity.y = JUMPFORCE

if !is_on_floor() and !jump_ani_playing:
	$Sprite.play("JUMP")
	jump_ani_playing = true


velocity = move_and_slide(velocity,Vector2.UP)
velocity.x = lerp(velocity.x,0,1)

:frowning:

shugishugi | 2020-07-28 19:58