my movement wont work properly.

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

basically whenever i press the left or right button its supposed to add acceleration and eventually get to max speed and when i let go its supposed to stop however its not working like that whenever i press it it stays on only the assign acceleration speed and whenever i click the left/right button acceleration is added its confusing.
script

extends KinematicBody2D

var MAX_SPEED = 500
var ACCELERATION = 5
var DASH_ACCELERATION = 500
var GRAVITY = 40
var DASH_STOP = 0
var JUMP_FORCE = 800
var JUMP_RELEASE_FORCE = 40
var FRICTION = 10

var velocity = Vector2.ZERO
func _physics_process(_delta):
	
	velocity.y += GRAVITY
	if velocity.length() > MAX_SPEED:
	  velocity = velocity.normalized() * MAX_SPEED
	velocity = move_and_slide(velocity)
	
		
	if Input.is_action_just_pressed("ui_right"):
if Input.is_action_just_pressed("ui_right"):
		velocity.x += ACCELERATION

	if Input.is_action_just_pressed("ui_left"):
		velocity.x -= ACCELERATION
		
	if Input.is_action_just_pressed("ui_up"):
		velocity.y -= JUMP_FORCE
		
	if Input.is_action_just_pressed("dash"):
		velocity.x += DASH_ACCELERATION
	if Input.is_action_just_released("dash"):
		velocity.x = DASH_STOP 
		
func accelerate(direction: Vector2):
	velocity = velocity.move_toward(MAX_SPEED * direction, ACCELERATION)
	
func apply_friction():
	velocity = velocity.move_toward(Vector2.ZERO, FRICTION)

Edited to fix forum code formatting. Though, as posted, the duplicate code and incorrect indentions here look suspect…

    if Input.is_action_just_pressed("ui_right"):
if Input.is_action_just_pressed("ui_right"):
        velocity.x += ACCELERATION

jgodfrey | 2023-05-24 22:00

:bust_in_silhouette: Reply From: Tom Mertz

I can’t quite tell how you want it to work by the code and it doesn’t look like in the script above you are calling either accelerate or apply_friction (maybe those aren’t relevant.

I did notice you are using is_action_just_pressed which only returns true on the first frame that pressed turned true. Check out the method’s doc description for more details. That means you’d only ever add acceleration one time when you first started pushing the button.

You might want is_action_pressed (doc link) which will return true if the action was just pressed and is still pressed every frame so that it’d keep adding your acceleration each frame until it hit the max

(Also as jgodfrey pointed out it looks like an error in your script with double ui_right check, but could just be a formatting error)