I have a Player Object with a child Sword Object. The Player has “Player” as tag, the Sword “Weapon”. I also have enemies, bearing the tag “Enemy”.
Whenever an Enemy hits the Players Body, the Player HP drops. Whenever the Sword hits an Enemy, the Enemy HP drops. However, whenever an Enemy touches the Sword the Player HP drops as well! Here is my code:
PlayerDamage.cs
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Player" && other.gameObject.tag != "Weapon")
{
other.gameObject.GetComponent<PlayerHealthController>().Damage(damage);
}
}
EnemyDamage.cs
void OnCollisionEnter2D(Collision2D other)
{
if ((other.gameObject.tag == "Enemy" || other.gameObject.name == "Enemy"))
{
other.gameObject.GetComponent<EnemyHealthController>().Damage(damage);
}
}
Is it because the Sword is a child of the Player?