need a little help with player movement

Question

here is my script, I don’t understand why it doesn’t work
extends CharacterBody2D

var speed = 150

var vel = Vector2()

func _physics_process(delta):
if Input.is_action_pressed(“left”):
vel.x -= speed
elif Input.is_action_pressed(“right”):
vel.x += speed
else:
vel.x = 0

move_and_slide()

replace left and right with ui_left and ui_right

1 Like

(You can properly format your code here by clicking the </> button and pasting your code between the backticks (```).)

It seems you have declared your own vel variable to track the velocity, but CharacterBody2D already has a velocity variable. Try removing your vel variable and setting velocity instead.

1 Like

Refer to the docs on move_and_slide here: Using CharacterBody2D/3D — Godot Engine (stable) documentation in English

1 Like