title: Recommended Approach to multiple colliders
body: <p>I am have some difficulty with what is probably a very silly thing. I have an enemy gameobject that depending on where it is hit (collision) - either it, or the play dies. I think the simplest way to describe this is by using the classic <em>Super Mario Bros.</em> game as an example.</p>
  
  <p>As you all know, if the player runs into the enemy - the player will lose - UNLESS he jumps on top of the enemy's head, in which case the enemy should die.</p>
  
  <p><img src="https://i.stack.imgur.com/fSMKf.png" alt="enemy with colliders"></p>
  
  <p>My initial idea was to create two colliders on the gameobject:</p>
  
  <ol>
  <li>Blue border represents a BoxCollider2D - that if collided with - will cause player to lose (notice it is slightly lower from the top)</li>
  <li>Green border represents a BoxCollider2D on a child gameobject - that if collided with - will cause the gameobject to die.</li>
  </ol>
  
  <p>The following is a simplified version of the code I used:</p>
  
  <pre><code>    // Collider #1
      public void OnCollisionEnter2D(Collision2D collision)
      {
          // Trigger 'Game-Over' logic
      }
  
      // Collider #2
      public void OnCollisionEnter2D(Collision2D collision)
      {
          if (collision.gameObject.tag == "Player")
          {
              Destroy(this.gameObject);
          }
      }
  </code></pre>
  
  <p>This kind-of works, however momentarily after colliding with <em>Collider #1</em>, <em>Collider #2</em> is also trigger - and while the enemy is destroyed, the player also loses.</p>
  
  <p>I have been playing with the RigidBody2D values to prevent the player from entering the 2nd collider when hitting the enemy from the top - but apparently with that force / speed, the colliders may be slightly inaccurate (or maybe I'm just doing it wrong?).</p>
  
  <p>I have looked into RayCasts but this seems too complex for something that me appears rather trivial (casting rays on all four sides and four vertices of the player - assuming that the player has a box collider).</p>
  
  <p>What I have resorted to 'for the moment' is a a single collider with a simple piece of code that I am unhappy with, and doesn't always work:</p>
  
  <pre><code>    public void OnCollisionEnter2D(Collision2D collision)
      {
          if (collision.gameObject.tag == "Player")
          {
              float yVelocity = collision.gameObject.transform.rigidbody2D.velocity.y;
  
              if (yVelocity &lt; 0)
              {
                  Debug.Log("Enemy will lose..." + yVelocity);
  
                  Destroy(this.gameObject);
              }
              else
              {
                  // Trigger 'Game-Over' logic
              }
          }
      }
  </code></pre>
  
  <p>I'm currently working in 2D mode, but solutions for 3D mode (while maybe more complicated than necessary for my question) will also be considered.</p>
  
  <p>Thanks guys.</p>