Why the animated jump not working when i press up the player stuck in the air

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Oussama099

extends KinematicBody2D

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

func _physics_process(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)
:bust_in_silhouette: Reply From: breeze4125

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.

Hi, i have the same issues too. I already tried ur solutions but it still doesn’t work. my line as same as He.

I’m beginner by the way, so hope you can gave me a couple of tips bout Godot :slight_smile:

Afiq_Hafizi | 2021-11-04 00:32