Search Unity

How to detect different colliders on one object

Discussion in 'Getting Started' started by Felipe_Schluepmann, Feb 20, 2021.

  1. Felipe_Schluepmann

    Felipe_Schluepmann

    Joined:
    Jan 12, 2021
    Posts:
    14
    Hi I'm making my first game a 3d platformer and I was wondering how could I detect when the player character is colliding with the head of the enemy (to kill him) or the body of the enemy(which kills the player).
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    OnCollisionEnter includes a Collision object, which includes an array called Collision.contacts. That is an array of contact points, and there should always be at least 1 in there. The contact point has a reference to the specific collider which was hit. I like to put tags on the child objects with the different colliders, so then I can just do this:

    Code (csharp):
    1. if (collision.contacts[0].thisCollider.gameObject.CompareTag("UnoriginalTag"))
    2. {
    3.     //Something unoriginal here
    4. }
    But you can use other ways, like keep a list of all the GameObject's colliders and then compare the collider to that list to find its index, or check if some Component is attached, etc,