0
votes

I wanted to make a simple prototype of a 2D character swinging a sword, so I attached the player tag to the parent and attached the sword as a child with a Weapon tag. I want the player to die when being touched by an enemy but the enemy to die when its being touched by the sword.

I have attached the respawning script to the player which detects when the enemy touches it, however it seems to also be triggered when the enemy is touching the sword child. I have tried to tell it to only trigger when interacting with this.tag Player but it didn't help.

Do I have to separate the sword completely or is something that could help me out here?

Here is the code I tried

if (other.tag == "Enemy" && this.tag == "Player")
{ 
   foreach (GameObject enemy in enemies)
   {
      enemy.GetComponent<EnemyAI>().EnemyRespawn();
   }
   Respawn();
}

Thank you for the help!

1

1 Answers

0
votes

have you made sure, that the weapon is outside of the player collider, or in other words: Is your player collider overlapping with the weapon, if so, then the OnCollisionEnter Method would be triggered at the right time, because visually the weapon and not the player would be touched, but the players collider as well.

I hope, that you understand, what I mean by that, if not, comment on this answer.

Greetings, Mario