I can't move diagonally

Godot Version

2.4.1.stable

Question

I can’t move diagonally with this code but I want to. Please help. I’m a beginner so sorry if this is a bad question.

extends CharacterBody2D

@onready var animations = $AnimationPlayer
@onready var sprite= $Sprite2D
@export var speed: int = 100
var current_dir = "none"

func playerMovement(delta):
	if Input.is_action_pressed("ui_right"):
		current_dir = "right"
		playAnimations(1)
		velocity.x = speed
		velocity.y = 0
	elif Input.is_action_pressed("ui_left"):
		current_dir = "left"
		playAnimations(1)
		velocity.x = -speed
		velocity.y = 0
	elif Input.is_action_pressed("ui_down"):
		current_dir = "down"
		playAnimations(1)
		velocity.x = 0
		velocity.y = speed
	elif Input.is_action_pressed("ui_up"):
		current_dir = "up"
		playAnimations(1)
		velocity.x = 0
		velocity.y = -speed
	else:
		playAnimations(0)
		velocity.x = 0
		velocity.y = 0
	
func playAnimations(movement):
	var dir = current_dir
	
	if dir == "right":
		sprite.flip_h = false
		if movement == 1:
			animations.play("walkRight")
		elif movement == 0:
			animations.play("idleRight")
			
	if dir == "left":
		sprite.flip_h = true
		if movement == 1:
			animations.play("walkRight")
		elif movement == 0:
			animations.play("idleRight")
			
	if dir == "down":
		sprite.flip_h = true
		if movement == 1:
			animations.play("walkDown")
		elif movement == 0:
			animations.play("idleDown")
			
	if dir == "up":
		sprite.flip_h = true
		if movement == 1:
			animations.play("walkUp")
		elif movement == 0:
			animations.play("idleUp")
		
		
	
	
	
func _physics_process(delta):
	playerMovement(delta)
	move_and_slide()

because of

so you can actually only move one direction at a time based on this kind of input process

try this youtube tutorial on creating 8 directions movement:

Hey
isn’t it because move_and_slide() is by default for platformer with gravity?

Looks like you can tweak motion_mode to create a top down way of moving

hope it helped!

In order to make your character capable of moving diagonally, you have to know a little about how movement is computed in a game application.

Whether it’s 2D or 3D, position is represented and stored in what is called a vector. In basic terms, a vector is a collection of numbers; two numbers in 2D, and three numbers in 3D. When visualizing a vector on a graph, a position vector sits between (0,0,0) and the position in question, (x,y,z)
image

Vectors are, however, not only used for position. Vectors can, and are, also used to represent directional quantities e.g. force, velocity, and so on. This is because vectors inherently contain information about two things: direction, and length.

For your intents and purposes, a vector may be used to represent the desired direction you want your character to move in. A simple example would be:

var inputX = Input.get_axis("ui_left", "ui_right")
var inputY = Input.get_axis("ui_up", "ui_down")

var direction = Vector2(inputX, inputY).normalized()

In the example above, the user’s input is used to construct a 2D vector. To maintain a length of 1, the vector is normalized using normalized().

To set the desired velocity for your character, the following code can then be used:

velocity = direction * speed

Notice how the directional vector, direction, is multiplied by a single number, speed. The multiplication is done to obtain a vector that has the direction of direction and the length of speed.
image

As a final note, games make heavy use of vectors and matrices. If you wish to become a better game creator, you have to study these mathematical concepts and learn to utilize them to your advantage.

EDIT: Here’s a link to a similar topic concerned with vectors.

I tried that video just now. It doesn’t even let me move because the friction is so high. I tried lowering it but then it wouldn’t stop.

I found an alternate way.