0 votes

When the player jumps it plays a animation("jumpL" if it's facing left or "jumpR" if it's facing right) and when he is just standing it plays another animation("idleL" if it's facing left or "idleR" if it's facing right). But when the player lands after he jumps he is stuck on the jump animation when he as supposed to be in the idle animation. I really don't know what is happening so if some on could help I would be very thankful.
CODE:

extends KinematicBody2D

#The original code was from the 2D Kinematic Character Demo
#Thaks for the code, 2D Kinematic Character Demo
onready var aniplayer = $AnimationPlayer
var ani
var ani_ref = "idleR"
var moving = false
const WALK_FORCE = 600
const WALK_MAX_SPEED = 400
const STOP_FORCE = 1300
const JUMP_SPEED = 200
var velocity = Vector2()
var gravity = 308

func _physics_process(delta):
    var walk = WALK_FORCE * (Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"))
    if abs(walk) < WALK_FORCE * 0.2:
            velocity.x = move_toward(velocity.x, 0, STOP_FORCE * delta)
    else:
            velocity.x += walk * delta # line 17-23 makes the player in the x axias

    velocity.x = clamp(velocity.x, -WALK_MAX_SPEED, WALK_MAX_SPEED)
    velocity.y += gravity * delta
    velocity = move_and_slide_with_snap(velocity, Vector2.DOWN, Vector2.UP)
    if is_on_floor() and Input.is_action_just_pressed("jump"):
            velocity.y = -JUMP_SPEED
    if Input.is_action_pressed("ui_left") or Input.is_action_pressed("ui_right") or Input.is_action_just_pressed("jump"):
        moving = true
    else:
        moving = false
    animiplay()
    print(velocity.y)
    print(moving)


func animiplay():
    if moving == true:
        if velocity.x >= 1:
            ani_ref = "walkR"
            if is_on_floor() == false:
                ani_ref = "jumpR"
                if is_on_floor():
                    if velocity.x >= 1:
                        ani_ref = "idleR"
        else:
            ani_ref = "idleR"
#BUG: if slightly tap the "ui_right" the walk animation will play even if moving = false 
        if velocity.x <= -1:
            ani_ref = "walkL"
            if is_on_floor() == false:
                ani_ref = "jumpL"
                if is_on_floor():
                    if velocity.x <= -1:
                        ani_ref = "idleL"
    elif moving == false:
        if is_on_floor():
            if velocity.x >= 1:
                ani_ref = "idleR"
            if velocity.x <= -1:
                ani_ref = "idleL"
    ani = ani_ref
    aniplayer.play(ani_ref)
Godot version 3.4.stable
in Engine by (15 points)

1 Answer

+2 votes
Best answer

Problem is here :

 func animiplay():
    if moving == true:
        if velocity.x >= 1:
            ani_ref = "walkR"
            if is_on_floor() == false:
                ani_ref = "jumpR"
                if is_on_floor():
                    if velocity.x >= 1:
                        ani_ref = "idleR"

You have gone too far with indentations. If player is not on floor it plays jumping animations and only then it checks, if it is on floor. Obviously he is not on floor, because it has just been checked, so your idle condition is never fulfilled. Code should go like this :

func animiplay():
        if moving == true:
            if velocity.x >= 1:
                ani_ref = "walkR"
                if is_on_floor() == false:
                    ani_ref = "jumpR"
                elif is_on_floor():
                    if velocity.x >= 1:
                        ani_ref = "idleR"

Instead of elif You can just use "else"

by (7,742 points)
selected by

Thanks it worked

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 Frequently asked questions and 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 [email protected] with your username.