Killing enemy jumping on his head

Hello everyone,
My 2D game is almost like Mario, and I’m having some problems to kill the enemies jumping on its head.

My enemy object has a collider2D with a onEnterCollision2D that do some damage to my player.

however my enemy object has a children, that is the head object. I’ve did this head object as a children so that way it could “follow” the body (enemy object).

My head object has a collider2D with a onEnterCollision2D that the player can destroy the enemy object jumping on it.

The problem is:
When i jump in the head i can kill the enemy, but stills lose health.

I thought that it can be a conflict about the 2 colliders, thinking that one is parent from the other.

Idea of solutions:

  • Stick somehow the head object with
    the collider in the enemy object with
    the collider, without doing
    parenting.
  • A way to avoid the children from
    getting the parents components.

Any Idea how should i solve my problem?

An object uses all of the colliders that it & its children have. You’re probably entering both sets of code. I would recommend this: Instead of having two colliders, in your collision function check which object’s center is higher (with an offset if needed), and either kill the enemy or damage the player based on that.

Alternatively, you could have a trigger collider (Collider2D with IsTrigger checked) in the player’s shoes and enemy’s head and a non-trigger collider in the enemy’s shoes and player’s head, with some empty space between them. As long as the empty space is not big enough for the sprites to pass through each other, it should be fine. In this instance, you would be damaging the player from OnCollisionEnter2D() and damaging the enemy from OnTriggerEnter2D(). The only downside here is that you’d have to add the bounce force manually after damaging the enemy.

In neither of these solutions does it help you to have the enemy’s head be a child of the enemy… But it doesn’t hurt you either, so do as you will.

Hello,
I have the same problem…

In my game2D, I have a Enemy with his collider (body of enemy) + his child GameObject with collider (head of enemy).

Here is a part of Enemy (body) script :

public void OnCollisionEnter2D(Collision2D collision)
    {
        if (col.transform.CompareTag("Player"))
        {
            //Si le joueur (player), saute sur la tête du boss,
            //ce dernier perd de la vie (-20) sur sa barre de vie.

            BossHealth bossHealth = boss.transform.GetComponent<BossHealth>();
            bossHealth.TakeDamage(damageOnCollision);
        }
    }

Here is a part of his child (head of enemy) script :

    private void OnCollisionEnter2D(Collision2D col)
    {
           if (col.transform.CompareTag("Player"))
           {
                    //Si le boss touche le joueur (player), ce dernier perd de la vie 
                    //(-20) sur sa barre de vie d'un total de (100).

                    PlayerHealth playerHealth = col.transform.GetComponent<PlayerHealth>();
                    playerHealth.TakeDamage(damageOnCollision);
           }
    }

But it doesn’t work! When the player collied with the (head of enemy collider), the player loose health !!!, like if he collid with the parent GameObject (the body of the enemy)… of course for testing, I increased the size of the enemy head collider, like this I’m sur that I collied with the head collider and not the body collider of enemy.

Did you found any solution for this problem?

Thanks in advance.