Did I do this wrong?

help me

I recently wrote code for a top-down game movement system. The left, right, up, and down codes are working, but when I press two buttons to go diagonally, it glitches out and plays the idle animation or a random frame from another movement direction. (Also, I don't know why there are so many extra spaces; please ignore them)

Please help me, this is the code:

extends CharacterBody2D

@export var speed = 200

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

var is_moving

func get_input():

var input\_direction = Input.get\_vector("ui\_left", "ui\_right", "ui\_up", "ui\_down")



velocity = input\_direction \* speed

func _physics_process(delta):

get\_input()



move\_and\_slide()





if Input.get\_vector("ui\_up","ui\_right","ui\_left","ui\_down"):



	is\_moving=true



else:

	is\_moving=false



if is\_moving==true:

	if Input.is\_action\_pressed("ui\_down") and Input.is\_action\_pressed("ui\_right"):

		animated\_sprite\_2d.play("runrightdown")



	if Input.is\_action\_pressed("ui\_down") and Input.is\_action\_pressed("ui\_left"):

		animated\_sprite\_2d.play("runleftdown")



	if Input.is\_action\_pressed("ui\_up") and Input.is\_action\_pressed("ui\_right"):

		animated\_sprite\_2d.play("runright")



	if Input.is\_action\_pressed("ui\_up") and Input.is\_action\_pressed("ui\_left"):

		animated\_sprite\_2d.play("runleft")



	if Input.is\_action\_pressed("ui\_up"):

		animated\_sprite\_2d.play("runup")



	if Input.is\_action\_pressed("ui\_left"):

		animated\_sprite\_2d.play("runleft")



	if Input.is\_action\_pressed("ui\_down"):

		animated\_sprite\_2d.play("run")



	if Input.is\_action\_pressed("ui\_right"):

		animated\_sprite\_2d.play("runright")



elif is\_moving == false:

	animated\_sprite\_2d.play("idle")



Posted Sep 2, 2025, 3:28 PM UTC

17
Loading comments…