0 votes

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.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 by (16 points)

1 Answer

0 votes

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,188 points)

can i ask how do i go about adding this?

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