Not fully moving when I try to move left or right

Godot 4.3

When I try to move my player left and right, the character will move a little bit to the side I want, but then will just stop moving until I press the key again. If I remove the else: command, I just slide without being able to stop.

Here is the code;

extends CharacterBody2D

var speed = 300
var jump_height = -300
var gravity = 1.3

func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor(): # tells if on floor
velocity += get_gravity() * delta * gravity # makes you go down

jumping

if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = jump_height

movement

if Input.is_action_just_pressed("left"):
	velocity.x = speed * -1
elif Input.is_action_just_pressed("right"):
	velocity.x = speed
else:
	Input.is_action_just_released("left") or Input.is_action_just_released("right")
	velocity.x = speed * 0


move_and_slide()

For movement, use is_action_pressed(), not is_action_just_pressed(). The just version means “did it just go from not-pressed to pressed?” while the not just version means “is the button currently down?”.

thanks it worked

1 Like

If you hit the “solution” button it will mark this question as solved.