OnCollisionEnter2D detect only the ground

Hi.
I have a simple question.
I have a “ground” layer with a Ground Object with an EdgeCollider to perform my ground (like Metal Slug).
Over this layer I have the others layer. One of them is “enemies”.

My enemy has a 2dBoxCollider and a 2d rigid boy.

I whant check if the “punch” of player, hit the Box Collider of the enemy.

void OnCollisionEnter2D(Collision2D coll) {
   string colliderName = coll.gameObject.name;
}

Unfortunally the method return only and always “ground” object.
Cleary the engine is doiing a correct analysys because the enemy is touching the ground (otherwise it would fall).

How can I check other collision?

Thanks

You can use one of those below methods:

  1. Use a new layer for detect damage collisions:
  • Add a child object into your enemy object(ex: named “DamageArea”)
  • Make a new layer(ex: “EnemyGetDamageAreaLayer”), change DamageArea object’s layer to “EnemyGetDamageAreaLayer”.
  • Make EnemyGetDamageAreaLayer collide with only character in Physics2DSettings
  • Add collider to your DamageArea object, make checkbox “Is Trigger” true.
  • Add script into DamageArea, use function OnTriggerEnter2D to detect character’s punch.
  • List item
  1. Use your “colliderName” to detect character (or game object tag instead of game object name), if it’s not character, do nothing.

I would try taking a look at layer based collision.