0 votes

Hi,
I'm coding a platformer game in godot, and the walking animation of the character is broken. I added the line: print($Sprite.animation) to check if it's running the proper animations, and I noticed that it changes animations every time.

Here is the code:
extends KinematicBody2D

var motion=Vector2()
const UP=Vector2(0,-1)
var moving

func _physics_process(delta):
    moving=false

    if (Input.is_action_pressed("ui_right") and is_on_floor()):
        motion.x+=50
        moving=true
        $Sprite.flip_h=false

    if (Input.is_action_pressed("ui_left") and is_on_floor()):
        motion.x+=-50
        moving=true
        $Sprite.flip_h=true 

    if (is_on_floor()):
        friction()

    motion.y+=9.80665
    if (is_on_floor()):
        if (Input.is_action_just_pressed("ui_up")):
            motion.y=-400
        else:
            motion.y=0

    if (is_on_wall()):
        motion.x=0

    animations()

    move_and_slide(motion, UP)  

func friction():
    var direction
    if (motion.x<0):
        direction="left"
    elif (motion.x>0):
        direction="right"
    else:
        direction="NOTHING TO SEE HERE"

    if (direction=="left"):
        motion.x+=40
        if (motion.x>0): motion.x=0

    if (direction=="right"):
        motion.x-=40
        if (motion.x<0): motion.x=0

func animations():
    if (motion.y>0 and not is_on_floor()):
        $Sprite.play("Fall")
    elif (motion.y<0):
        $Sprite.play("Jump")
    elif (moving):
        $Sprite.play("Walk")
    elif (not moving):
        $Sprite.play("Stand")

    print($Sprite.animation)

Could you please tell me where the bug is?

Thanking in advance,
Andrew27

asked May 2 in Engine by Andrew27 (12 points)
edited May 2 by Andrew27

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.