Hi. I have a problem with registering hits on an enemy

Enemy and player attacks are implemented almost identically, only when the player hits the enemy, nothing happens. please help me i don’t understand how it works.

project:

I would be nice if you added your code to your post. Makes it easier to inspect.

There is not enough information in the video to say exactly what the problem is. But since _on_hitbox_area_entered is not called, you should check the following:

  • Is it connected to the signal?
  • Is the enemy an Area2D? If not you have to use body_entered instead of area_entered.
  • Are the collision layers and mask set up correctly?

yes the enemy is area2D
collisions and masks are set up correctly

this video shows collisions and masks

I can’t see the collision layers and masks in your video. Did you connect the function to the area_entered signal of the hitbox?

Is your enemy a Area2D or a CharacterBody2D? If they have a “HurtBox” like the player does then that hurtbox needs to be in the group “enemies”. The group “enemies” also must be spelled with the exact same capitalization as the group is defined.

Note that the way you set up your global signal means when any one enemy is attacked, every enemy will take damage. It’s probably better to use the area parameter to deduce which enemy was just attacked and call their _on_damaged function from the hitbox’s function, rather than using signals.


To paste scripts in the forum use three ticks

```
func hello() -> void:
    print("Hello")
```

becomes:

func hello() -> void:
    print("Hello")
1 Like

enemy is a CharacterBody2D.
I don’t know how to make characters differently, so. If it’s not too much trouble, I attached a link to the git. As far as I understand, for some reason the _on_hit_box_area_entered function doesn’t work for the player.

I took a look at your project. It looks like you want the player hitbox to scan for the enemy’s hurbox (at least you do the same in the enemy). For this you have to set the player’s hit-box to scan for enemies hurt-box layer (in your case layer ‘6’). Specifically, you have to set the collision mask of the player’s hit-box to ‘6’, not the layer. I suggest you read about collision layers and masks (see the link I posted before).

As some heads up, the next problem you will probably encounter is that the hurt-box of the enemy is not in the group ‘enemy’.
One solution to this could be to have the enemy handle the ‘attack’ intersection and then inform the player with the enemy information from there.

While skimming you code I saw that you currently have signals in the your ‘Signal Autload/Bus’ that should not be in there (or should not be signals at all). E.g., as @gertkeno mentioned, instead of enemy_attack you should directly apply the damage to the hit enemy. I also suggest that instead of emitting the player_position signal every tick, you get the reference to player to enemy (e.g., with autoload).

1 Like

thanks everyone

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.