my character moves on his own when i press left or right once and he gets really fast like sonic

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

this is my script

extends KinematicBody2D

var score : int = 0

var speed : int = 300
var jumpForce : int = 500
var gravity : int = 800

var vel : Vector2 = Vector2()

func _physics_process(delta):

# movement inputs
if Input.is_action_pressed("move_left"):
	vel.x -= speed
if Input.is_action_pressed("move_right"):
	vel.x += speed

vel = move_and_slide(vel, Vector2.UP)

if vel.x < 0:
	$Sprite.flip_h = true
elif vel.x > 0:
	$Sprite.flip_h = false

vel.y += gravity * delta

# jump input
if Input. is_action_just_pressed("jump") and is_on_floor():
	vel.y -= jumpForce
:bust_in_silhouette: Reply From: Inces

You didn’t code him stopping. Look at your code - you press left once, vel becomes -speed, you let go of a button and vel is still -speed. When You press button again and hold it, every frame player is accelerated by amount of speed.

if input is left (.....)
elif input is right (....)
else: vel = 0

vel = clamp(-maxspeed,vel,maxspeed)