0 votes

I have a Area2D hitbox set up to emit signals on body enter/exit and use it to hit enemies. The hitbox is always there and I check if there is an enemy inside when performing my hit/attack. It works ok, but it cannot hit multiple enemies, due to keeping track of only one enemy with the body enter/exit.
How do people solve this issue? Should I...
- make an array that keeps track of all bodys entering/exiting?
- disable/enable the Area2D, somehow, I tried this unsuccessfully...
- something else?
- create a brand new hitbox from code and hit all that enters, destroy box shortly after?

I know that I could animate a box to appear/dissapear to get that effect but I see myself not wanting to fidget around with animation player for each new hitbox as the project grows, I want all the manipulations to be in code.

Godot version 3.2.3
in Engine by (22 points)

1 Answer

0 votes

Assuming hitbox is your Area2D, at the time of the attack do:

 for body in hitbox.get_overlapping_bodies():

or connect the body_entered signal and damage the bodies as they come in

func _on_Area2D2_body_entered(body: Node) -> void:

I'm not sure which works best for your game.
obligatory: make sure your collision layers are set up properly

by (2,190 points)

I tried the .getoverlappingbodies() thing earlier but it did not work satisfactory, seems like that function is not "instant"?
What I ended up doing was having the collision shape in the area2D disabled by default and enabling it when performing the attack. It works perfectly.

func attack():
attack = true
weaponhitbox.disabled = false
timer = Timer.new()

func attacktimer(): #runs when timer trigger
attack = false
weaponhitbox.disabled = true

func onWeaponHitboxbodyentered(body):
enemy = body
enemy = null

Indeed the get_overlapping_bodies() is not instant, it updates once a physics frame. If you are processing your inputs during _process() you can consider changing them to be processed during _physics_process() instead. The only thing I would change about your code is:

func _on_WeaponHitbox_body_entered(body):
    if body.has_method("_take_damage"):

This will prevent crashes down the line if you decide to have your weapon interact with say pots or something down the line.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.