How to avoid hitting two NPCs?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Skydome

Hi so when I punch or kick. The NPC standing behind’s hitbox gets activated too. I was wondering if this could be stopped.
The pictures will explain what I mean.enter image description hereenter image description here

If this can’t be helped. Do you think it’ll ruin the game? I have noticed this happening in some professional games too.

:bust_in_silhouette: Reply From: magicalogic

Use collision layers. Refer to the documentation here.

Thanks. I know about that but that won’t help here unfortunately.

Skydome | 2021-05-30 11:40

:bust_in_silhouette: Reply From: Yaann

I think one way you could do that is to deactivate the hitbox the moment your character hits an enemy so that the next enemy doesn’t get hit, of course you’ll have to activate the hitbox again everytime you have to hit (with an AnimationPlayer for example), I’m not too much into GDScript but the function of your hitbox signal should look like something like this:

func on_hit(body):
   if(body is enemy):
      hitbox.CollisionShape.disabled = true

Yea that’s a good solution. I was thinking something similar.
There are problems to this as sometimes the NPC standing behind gets hurt first with long range attacks like spears and stuff.
The second problem is that hit box activation and deactivation was done with animation player. Which was much simpler. It can be done with script too though.

I’m going to put off this issue for later. Do you think it’ll make the game bad? I’ve noticed this happening with studio made games as well. And 3D games too. I’m wondering if this is something I even need to fix as it won’t be noticeable a lot in the game.

Skydome | 2021-05-30 11:49

For long-range attacks you can tweak the hitbox so it only hits the enemy in front of you, a hitbox isn’t obliged to cover the whole weapon or put it on a tip so just keep working on it until you find a satisfying result.
You can mix hitbox deactivation with a script and an animation player.
Personally, I don’t think it’s going to make the game bad neither should you really think about it as long as it’s not too broken, if studio games get away with it why not you?

Yaann | 2021-05-30 11:57