New user, need help wtih code

godot v4

Hi new user/coder here, been following some tutorials and the character is stuck in one place and wont move apart from turning to each side. Any help would be appreciated.

extends CharacterBody2D

const speed = 100
var current_dir = “none”

func _ready():
$AnimatedSprite2D.play(“front_idle”)

func _physics_process(delta):
player_movement(delta)

func player_movement(delta):

if Input.is_action_just_pressed("ui_right"):
	current_dir = "right"
	play_anim(1)
	velocity.x = speed
	velocity.y = 0
elif Input.is_action_just_pressed("ui_left"):
	current_dir = "left"
	play_anim(1)
	velocity.x = -speed
	velocity.y = 0
elif Input.is_action_just_pressed("ui_down"):
	current_dir = "down"
	play_anim(1)
	velocity.y = speed
	velocity.x = 0
elif Input.is_action_just_pressed("ui_up"):
	current_dir = "up"
	play_anim(1)
	velocity.y = -speed
	velocity.x = 0
else:
	play_anim(0)
	velocity.x = 0
	velocity.y = 0
	
move_and_slide()

func play_anim(movement):
var dir = current_dir
var anim = $AnimatedSprite2D

if dir == "right":
	anim.flip_h = false
	if movement == 1:
		anim.play("side_walk")
	elif movement == 0:
		anim.play("side_idle")
		
if dir == "left":
	anim.flip_h = true
	if movement == 1:
		anim.play("side_walk")
	elif movement == 0:
		anim.play("side_idle")
		
if dir == "down":
	anim.flip_h = true
	if movement == 1:
		anim.play("front_walk")
	elif movement == 0:
		anim.play("front_idle")
		
if dir == "up":
	anim.flip_h = true
	if movement == 1:
		anim.play("back_walk")
	elif movement == 0:
		anim.play("back_idle")

it is because you are using “is_action_just_pressed” u need to use “is_action_pressed” instead just pressed checks if u just pressed it so it only moves it once.

also it might be a better idea to use Vector2 instead of strings for current_dir so u would not have to check for each dir in play_anim

1 Like