Why isnt my movement code working?

I am pretty new to coding and have been doing my best by learning form vidieos. But my code still isnt working

extends CharacterBody2D

var speed = 200
var velpcity = Vector2.ZERO

func _ready():
pass

func _physics_process(_delta):
velocity = Vector2()

if Input.is_action_just_pressed('ui_down'):
	velocity.y += speed
if Input.is_action_just_pressed('ui_up'):
	velocity.y -= speed
if Input.is_action_just_pressed('ui_left'):
	velocity.x -= speed
if Input.is_action_just_pressed('ui_right'):
	velocity.x += speed

move_and_slide()

is_action_just_pressed Only checks if the action was pressed this frame not if it was held down. You could use is_action_pressed or a more applicable function like get_vector

func _physics_process(_delta):
    velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") * speed
    move_and_slide()

get_vector translates all the inputs listed into a Vector2, left/right are negative and positive on the X axis, up/down are negative and positive on the Y axis.

1 Like

thank you so much!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.