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?