My 2d character doesn't move left properly

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

Hello, so im making a simple 2d platformer, But I have and issue getting my character to move left, im using the “lerp” function to stop the character’s movement when left or right keys are released moving right works like a charm, but i can’t get my character to move left, it only moves when i hit the key repeatedly.

here’s the code:

extends KinematicBody2D

var velocity = Vector2(0,0)

func _physics_process(delta):
	if Input.is_action_pressed("right"):
		velocity.x = 100
	if Input.is_action_just_pressed("left"):
		velocity.x = -100
		
	move_and_slide(velocity)
	
	velocity.x = lerp(velocity.x,0,0.1)
:bust_in_silhouette: Reply From: Ertain

The problem is in the lerp() function. The function move_and_slide() should be used like this:

...
# This is how I learned the basics of 2D movement.
velocity.x = 0
if Input.is_action_pressed("right"):
    # Note the "+=" operator here.
    velocity.x += 100
if Input.is_action_just_pressed("left"):
    velocity.x += -100

velocity = move_and_slide(velocity)