Home > Blockchain >  How to kill only one enemy when the bullet (arrow) collides with multiple colliders
How to kill only one enemy when the bullet (arrow) collides with multiple colliders

Time:07-29

I am new to Unity2D. I am trying to make castle defense game. When the spawners start to inheritance the enemies overlap (they should), but when the archer arrow collides whit the enemies it kills them all. I searched everywhere for the answer of this but nothing... My questions is: Is there a way to only hit one target at time? I am not sure what code I should share with you to better understand what I want so if you need something tell me. Thanks!

CodePudding user response:

its hard to say with the information given, based on what you have said it seems that all the entity's are just 1 entity(so when you kill 1 enemy you kill the only enemy which is all of them). you can have them run independently from each other.

CodePudding user response:

Maybe you can use collision enter function to check bullet is hitting enemy's body. You can use this code below...

private bool isEntered = false;
void OnCollisionEnter(Collision collision)
{
    if(isEntered) return;
    if(collision.gameObject.tag == "enemy") isEntered = true;
    ....
    ....
}

I hope it will work for you.

  • Related