How do I correct my input key if its moving left when its set to the right arrow key?

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

Hello, I’m following this tutorial but my situation is my robot won’t move right instead it’ll move left(even though it’s set to the right), both my up input key and left input key do what intended. Below I have the pdf I’m using to learn(page 24), and the code pasted from the project.


extends KinematicBody2D


var score : int = 0


var speed : int = 200
var jumpforce : int = 600
var gravity : int = 800

var vel : Vector2 = Vector2()
var grounded : bool = false


onready var sprite = $sprite

func _physics_process (delta):

reset horizontal velocity

vel.x = 0

movement inputs

if Input.is_action_pressed("move_left"):
	vel.x -= speed
if Input.is_action_pressed("move_right"):
	vel.x -= speed

applying the velocity

vel = move_and_slide(vel, Vector2.UP)


vel.y += gravity * delta

jump input

if Input.is_action_pressed("jump") and is_on_floor():
	vel.y -= jumpforce

sprite direction

if vel.x < 0:
	sprite.flip_h = true
elif vel.x > 0:
	sprite.flip_h = false
:bust_in_silhouette: Reply From: jgodfrey

The code for your move_right input is wrong. It should be:

if Input.is_action_pressed("move_right"):
    vel.x += speed

Notice that the speed variable needs to be added (+=) to velocity, not subtracted (-=)

I hope to be able to catch myself next opportunity, thank you for your observation. I appreciate it

EugenioTC | 2022-11-06 17:56