I was making movement system and when I run the char just keeps going to the bottom right. why does it happen?

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

extends CharacterBody2D

@export var speed: int = 50
@onready var animations = $AnimationPlayer

var isAttacking = false

func _physics_process(delta):
handleInput()
updateAnimations()
move_and_slide()
#last key pressed
var last_pressed = “Down”
if Input.is_action_pressed(“ui_up”):
last_pressed = “Up”
elif Input.is_action_pressed(“ui_left”):
last_pressed = “Left”
elif Input.is_action_pressed(“ui_right”):
last_pressed = “Right”
elif Input.is_action_pressed(“ui_down”):
last_pressed = “Down”

#velocity
velocity.x = speed
velocity.y = speed

#if not attacking
if isAttacking == false:
	var direction = "Down"
	if velocity.x > 0:
		direction = "Right"
	elif velocity.x < 0:
		direction = "Left"
	elif velocity.y < 0:
		direction = "Up"
	animations.play("walk" + direction)
#if attacking
elif isAttacking == true and Input.is_action_pressed("attack"):
	var direction = last_pressed
	animations.play("attack" + direction)

func updateAnimations():
if isAttacking == false:
if velocity.length() == 0:
if animations.is_playing():
animations.stop()

func handleInput():
var moveDirection = Input.get_vector(“ui_left”, “ui_right”, “ui_up”, “ui_down”)
velocity = moveDirection*speed

#velocity
velocity.x = speed
velocity.y = speed

Do you know what’s happening here? The code takes the direction from Input but then sets the direction to the speed. Remove this, your code will behave differently (although I can’t guarantee success!)

spaceyjase | 2023-04-24 10:25