Diagonal Movement Not Working Using "move_and_slide()"

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

I am working on a Space Invaders clone type of game. I used “move_and_slide” as the player’s movement function. The problem is that diagonal input (like UP and RIGHT pressed together) don’t seem to do anything. Please help as I want 8-way movement in my game. My code:

func _physics_process(delta):

var DOWN = Input.is_action_pressed("ui_down")
var UP = Input.is_action_pressed("ui_up")
var LEFT = Input.is_action_pressed("ui_left")
var RIGHT = Input.is_action_pressed("ui_right")



if DOWN:
	velocity = Vector2(0, speed)
	move_and_slide(velocity)
elif UP:
	velocity = Vector2(0, -speed)
	move_and_slide(velocity)
elif RIGHT:
	velocity = Vector2(speed, 0)
	move_and_slide(velocity)
elif LEFT:
	velocity = Vector2(-speed, 0)
	move_and_slide(velocity)
:bust_in_silhouette: Reply From: CynicalKazu

Never mind, it was a noob mistake. I had used elif so two inputs at the same time were never checked for. I changed all elifs to if and now it works.

If that’s what you want for movement then don’t use any else ifs

Merlin1846 | 2019-11-27 23:13

Thank you HAHA XD

S4_Yuuki | 2022-04-07 17:51