my character's jump animation don't work with move right and move left with same time. i don't find where is problem.

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

extends KinematicBody2D

const SPEED = 100
const GRAVITY = 10
const JUMP_POWER = -250
const FLOOR = Vector2(0, -1)

const ARROW = preload(“res://world/Arrow.tscn”)

var velocity = Vector2()

var is_attacking = false

var on_ground = false

func _physics_process(delta):

if Input.is_action_pressed("ui_right"):
	if is_attacking == false:
		velocity.x = SPEED
		$AnimatedSprite.play("Run")
		$AnimatedSprite.flip_h = false
		if sign($Position2D.position.x) == -1:
			$Position2D.position.x *= -1
	
elif Input.is_action_pressed("ui_left"):
	if is_attacking == false:
		velocity.x = -SPEED
		$AnimatedSprite.play("Run")
		$AnimatedSprite.flip_h = true
		if sign($Position2D.position.x) == 1:
			$Position2D.position.x *= -1
	
	
else:
	velocity.x = 0
	if on_ground == true && is_attacking == false:
		$AnimatedSprite.play("Idle")

if Input.is_action_pressed("ui_up"):
	if is_attacking == false:
		if on_ground == true:
			velocity.y = JUMP_POWER
			on_ground = false
		
		
if Input.is_action_just_pressed("ui_accept") && is_attacking == false:
	if is_on_floor():
		velocity.x = 0
	is_attacking = true
	$AnimatedSprite.play("Attack")
	var arrow = ARROW.instance()
	if sign($Position2D.position.x) == 1:
		arrow.set_arrow_direction(1)
	else:
		arrow.set_arrow_direction(-1)
	
	get_parent().add_child(arrow)
	arrow.position = $Position2D.global_position
velocity.y += GRAVITY

if is_on_floor():
	if on_ground == false:
		is_attacking == false
	on_ground = true
else:
	if is_attacking == false:
		on_ground = false
		if velocity.y < 0:
			$AnimatedSprite.play("Jump")
velocity = move_and_slide(velocity, FLOOR)
:bust_in_silhouette: Reply From: Aireek

If I had to guess it’s because In physicsprocess(delta) it runs multiple times per second. So even though you are in the air, you are still holding down the Left or Right key. So it instantly overrides the jump animation with the run animation. Basically… it did technically work with left and right. It’s just it switched back to the running animation so fast you probably didn’t see it lol. Maybe try stopping the previous animation until jump animation has finished or until on_ground becomes true again. Sorry I don’t have a Code-based answer. Hopefully someone more knowledgeable can give a more direct answer, or you figure it out now. good luck!

Edit: see comment below

This is what I was talking about

if Input.is_action_pressed("ui_right"):
    if is_attacking == false:
        velocity.x = SPEED
        if on_ground == true:
             $AnimatedSprite.play("Run")
             $AnimatedSprite.flip_h = false
        if sign($Position2D.position.x) == -1:
             $Position2D.position.x *= -1

You could try to add a on_ground = true line to your $animatedsprite.play (“run)” line in your movement function. So it will only play that animation if hes on the ground.

Aireek | 2020-04-14 17:53