Why KinematicBody2d doesn't move to the right, only to the left with standard Input.is_action_pressed?

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

Hi, guys, i have a weird issue on my platformer project: the kinematicbody2d is not moving to the right when it is on floor with the is_action_pressed comand. Here is the code:

extends KinematicBody2D

const GRAVITY=20
const SPEED=300
const JUMP=-600
const UP=Vector2(0,-1)
var motion=Vector2()

func _physics_process(delta):
motion.y+=GRAVITY

if Input.is_action_pressed("ui_right"):
	motion.x=SPEED
	$AnimatedSprite.flip_h=false
	if is_on_floor():
		$AnimatedSprite.play("walk")
if Input.is_action_pressed("ui_left"):
	motion.x=-SPEED
	$AnimatedSprite.flip_h=true
	if is_on_floor():
		$AnimatedSprite.play("walk")
elif is_on_floor():
	motion.x=0
	$AnimatedSprite.play("idle")
	
if is_on_floor() and Input.is_action_just_pressed("ui_up"):
	motion.y=JUMP
	
if is_on_floor() and Input.is_action_just_pressed("attack"):
	$AnimatedSprite.play("attack1")

if !is_on_floor():
	$AnimatedSprite.play("jump")

motion=move_and_slide(motion,UP)
:bust_in_silhouette: Reply From: hilfazer

This code

elif is_on_floor():
    motion.x=0

will be called if "ui_left" action is not pressed, regardless of "ui_right".

Use elif in this line:

if Input.is_action_pressed("ui_left"):

Sorry, but it still doesn’t work or maybe i don’t get it. Could you write the line for example? Thank you for your help.

cristixxx | 2018-03-10 10:22

This line

elif Input.is_action_pressed("ui_left"):

instead of this one

if Input.is_action_pressed("ui_left"):

hilfazer | 2018-03-10 10:28

It works, thanks.

cristixxx | 2018-03-10 11:38