0 votes

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 physicsprocess(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)
in Engine by (12 points)

2 Answers

0 votes

Hi, try to use this in the function _process.

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

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

Try with this:

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

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.

by (72 points)

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?

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 ;) ).

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.

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 physicsprocess(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)

:-(

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.