Hitbox doesn't change direction

Godot Version

Godot 4.4.1

Question

Hello again. I’m currently trying to make a game, following a tutorial. But I’ve been having problems related to the player hitbox. It’s supposed to change direction depending where the player is facing, but this doesn’t happen. Here are all the codes related to the hitbox.

extends CharacterBody2D


const SPEED = 300.0

var last_direction: Vector2 = Vector2.RIGHT
var is_attacking: bool = false
var hitbox_offset: Vector2

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var swingsword: AudioStreamPlayer2D = $swingsword
@onready var hitbox: Area2D = $Hitbox


func _ready() -> void:
	
	hitbox_offset = hitbox.position
	

The variables first.

func process_movement() -> void:
	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction := Input.get_vector("left", "right", "up", "down")
	
	
	if direction != Vector2.ZERO:
		velocity = direction * SPEED
		last_direction = direction
		update_hitbox_offset()
	else:
		velocity = Vector2.ZERO
		
func update_hitbox_offset() -> void:
	var x := hitbox_offset.x
	var y := hitbox_offset.y
	
	
	match last_direction:
		Vector2.LEFT:
			hitbox.position = Vector2(-x, y)
		Vector2.RIGHT:
			hitbox.position = Vector2(x, y)
		Vector2.UP:
			hitbox.position = Vector2(y, -x)
		Vector2.DOWN:
			hitbox.position = Vector2(-y, x)

What could be done?

is your game top down ? or side view ?

Are you ensuring that last_direction is actually equal to these values? It looks like you take direction which can range from 0 to 1, this doesn’t seem like a sensible way to do this, instead I’d suggest using checks against the x and y axes

Top down.

I was following a tutorial, so I’m unsure how to do otherwise.

here you should add numbers like :

add a suitble number instead of 300

match last_direction:
		Vector2.LEFT:
			hitbox.position = Vector2(-300, 300)
		Vector2.RIGHT:
			hitbox.position = Vector2(300, 300)
		Vector2.UP:
			hitbox.position = Vector2(300, -300)
		Vector2.DOWN:
			hitbox.position = Vector2(-300, 300)