+1 vote

extends KinematicBody2D

var velocity = Vector2(0,0)
const speed = 180
const gravity = 30
const jumpforce = -800

func physicsprocess(delta):

if Input.is_action_pressed("ui_right"):
    velocity.x = speed
    $Sprite.play("run")
    $Sprite.flip_h = false
elif Input.is_action_pressed("ui_left"):
    velocity.x = -speed
    $Sprite.play("run")
    $Sprite.flip_h = true
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,0.2)
in Engine by (13 points)

1 Answer

+2 votes

This doesn't work because this if/elif/else statement you have here:

if Input.is_action_pressed("ui_right"):
   ...
elif Input.is_action_pressed("ui_left"):
   ...
else:
   ...

will always call $Sprite.play (since you have an else statement).

That means when you are in the air, you are calling $Sprite.play("jump") but then also calling "walk" or "idle" on the very next frame. So your AnimatedSprite is going to be constantly switching back and forth between two animations, which is why it freezes up like that.

To fix this, you could just put the "jump" call at the top, like so:

if not_is_on_floor():
     Sprite.play("jump")
elif Input.is_action_pressed("ui_right"):
     ...
// and so on

Hope this helps.

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