0 votes

I am new to Godot. So, to learn I coded 2D platformer movement for my player by following this tutorial and I found a problem that my character is not playing Jump and Fall animation when I press jump but other animations are working fine for me.


extends KinematicBody2D

const UP_DIRECTION := Vector2.UP

export var speed := 50
export var gravity := 5000
export var jump_strength := 800.0
var _velocity := Vector2.ZERO

onready var pivot: Node2D = $AnimatedSprite
onready var _start_scale: Vector2 = pivot.scale

export var maximum_jumps := 1
var jumps_made := 0 

func _physics_process(delta: float) -> void:
    var horizontal_movement = (
        Input.get_action_strength("move_forward")
        - Input.get_action_strength("move_backward")
        )

    _velocity.x = horizontal_movement * speed
    _velocity.y += gravity * delta


    var is_jumping := Input.is_action_just_pressed("jump") and is_on_floor()
    var is_moving :=  not is_zero_approx(_velocity.x)
    var is_idling := is_zero_approx(_velocity.x)
    var is_falling := _velocity.y > 0.0 and not is_on_floor()

    if is_jumping:
        jumps_made = 1
        _velocity.y = -jump_strength


    elif is_idling:
        jumps_made = 0

    elif is_moving:
        jumps_made = 0


    _velocity = move_and_slide(_velocity, UP_DIRECTION)

    if not is_zero_approx(_velocity.x):
        pivot.scale.x = sign(_velocity.x) * _start_scale.x
    if is_jumping:
        $AnimatedSprite.play("Jump")
    if is_falling:
        $AnimatedSprite.play("Fall")
    if is_idling:
        $AnimatedSprite.play("Idle")
    if is_moving:
        $AnimatedSprite.play("Walk")
Godot version v3.3.stable.official
in Engine by (14 points)
reshown by

Hi,
Does the character jump though, even though the animation isn't played?
Is the animation in the sprite called Jump?
I can follow the code, but can't test it without creating a character etc etc, so if you could upload the project I'd be happy to take a look and try and help.

I would say the most likely answer is that you have used different cases on the animation. If the animation doesnt have a capital j for example then there is nothing to call with the name Jump.

Sorry for late response but i already fixed it. The problem was I was using if statement instead of elif.

if is_jumping:
    $AnimatedSprite.play("Jump")
if is_falling:
    $AnimatedSprite.play("Fall")
if is_idling:
    $AnimatedSprite.play("Idle")
if is_moving:
    $AnimatedSprite.play("Walk")

Now I changed that into this.

if is_jumping:
        $AnimatedSprite.play("Jump")
    elif is_falling:
        $AnimatedSprite.play("Fall")
    elif is_idling:
        $AnimatedSprite.play("Idle")
    elif is_moving:
        $AnimatedSprite.play("Walk")

Please log in or register to answer this question.

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.