Health, damage in a 2D game

The enemy is killed, if you jump on it, on contact, damage will be done to me and the enemy, how can I make sure that only the enemy is dealt? what do you need to use for this?

I managed to achieve that only the enemy was dealt, but now the enemy deals damage in one direction, how to do it in the other direction? The script is applied to the enemy and the player

public class CollisionDamage : MonoBehaviour
{
    public int damage = 10;
    public string collisionTag;

    GameObject enemy;
    GameObject player;

    private void OnCollisionEnter2D(Collision2D coll)
    {
        Health healthE = enemy.GetComponent<Health>();
        Health healthP = player.GetComponent<Health>();

        if (coll.gameObject.CompareTag(collisionTag)) //tag enemy
        {
            foreach (ContactPoint2D point2D in coll.contacts)
            {
                if (point2D.normal.y >= 0.5f)
                {
                    //Destroy(enemy.gameObject);                   
                    healthE.takeDamage(damage);
                }
                else if (point2D.normal.x >= 0.5f)
                {
                    healthP.takeDamage(damage);
                }
            }
        }
    }

    private void Start()
    {
        enemy = GameObject.FindWithTag("Enemy");
        player = GameObject.FindWithTag("Player");  //GameObject.FindGameObjectWithTag("Player");
    }
}

Are you aware of what these two checks imply?

I would theorize this might have something to do with the directionality of damage.

1 Like

thanks to the first check I can inflict damage from above, thanks to the second opponent damages the player from the side, right?