Hello there, I’m new in Godot and programming in general.
I’m trying to make my character attack hitbox to move dinamically based on mouse position. It certenly works with flipping the sprite2d, but the area 2d is kinda buggy.
Here is the code:
@onready var sprite: Sprite2D = $Sprite
@onready var auto_hit_box: Area2D = $AutoHitBox
@onready var auto_attack_cd: Timer = $AutoAttackCD
var _autoattack_cd: bool = false
func _physics_process(delta: float) -> void:
velocity.y += delta * GRAVITY
if Input.is_action_just_pressed("auto-attack"):
call_deferred("basic_attack")
func basic_attack() -> void:
if _autoattack_cd == false:
sprite.flip_h = get_global_mouse_position().x < global_position.x
auto_hit_box.position.x = -auto_hit_box.position.x if get_global_mouse_position().x \
< global_position.x else auto_hit_box.position.x
_autoattack_cd = true
auto_attack_cd.start()
As I show in the video, the area of the hitbox goes from right to left when I attack. If I have an enemy on my left and I click to my right, the enemy of my left dies, and viceversa. I want the hitbox position to stay on my right or left depending on where I just clicked, so I kill the proper enemy. It’s more clear watching the video I guess, maybe I can’t explain properly because english ain’t my native language sorry
If the mouse is on the left side you were flipping the existing x position of the hitbox regardless of which side it currently is. And if the mouse is on the right side, you were not flipping at all.
You need to use positive/negative constant instead of current position.