Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Problem with Colliders

Discussion in 'Physics' started by Ivagolas, Nov 10, 2020.

  1. Ivagolas

    Ivagolas

    Joined:
    Oct 18, 2020
    Posts:
    4
    Hello there! I'm having troubles with colliders, I'm not an english native speaker so sorry if I make a mistake, so this is my problem:

    I have an enemy elf (Elfito Verde) that has his own Rigidbody and his own Collider2D. I created an empty child object (Detector Enemigo) that has another collider that is a trigger and its bigger than the elf collider because I want its collider to detect the player's character. They both have different scripts, the elf has a script that use OnCollisionEnter2D() to detect collisions and change its movement to the other side, and the other has a OnTriggerEnter2D to detect the player and attack.

    Now my first problem was that both colliders activate the OnCollisionEnter2D() and my elf turn around when the biggest collider find the player. So I search and I found that I should use a kinematic Rigidbody to make the parent and the child's colliders to not work as a compound collider, so I did that. My problem is that even with a Rigidbody attached to the child gameobject, the colliders still works as a compound collider.

    How can I make the child collider to work individualy and not as a compound collider?

    Thanks for your help!
     

    Attached Files:

  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Saca el collider de elfito verde para otro hijo. Si haces el OnTriggerEnter2D desde un padre, este también detecta las colisiones del hijo.
     
  3. Ivagolas

    Ivagolas

    Joined:
    Oct 18, 2020
    Posts:
    4
    No quería tener que hacer eso porque tendría que cambiar un montón de cosas del script del elfito por eso buscaba una mejor solución, pero parece que tendré que hacerlo.
     
  4. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    A ver, también podrías hacer también una criba y comprobar que el collider sea el apropiado:

    Code (CSharp):
    1.  
    2. public class Elfito : MonoBehaviour
    3. {
    4.     BoxCollider2D _elfitoCollider;
    5.  
    6.     private void Awake()
    7.     {
    8.         _elfitoCollider = GetComponent<BoxCollider2D>();
    9.     }
    10.  
    11.  
    12.     private void OnTriggerEnter2D(Collider2D other)
    13.     {
    14.         Collider2D[] triggeredColliders = null;
    15.         other.OverlapCollider(new ContactFilter2D(), triggeredColliders);
    16.         for(int i= 0; i < triggeredColliders.Length; i++)
    17.         {
    18.             if(triggeredColliders[i] == _elfitoCollider)
    19.             {
    20.                 //Tu elfito trigger
    21.                 return;
    22.             }
    23.         }
    24.     }
    25. }
    Pero es más lento y consume más recursos, no te lo recomiendo. Échale un vistazo a la estructura ContactFilter2D. Te puede servir para hacer tus propios OnTriggerEnter personalizados.
     
    Ivagolas likes this.
  5. Ivagolas

    Ivagolas

    Joined:
    Oct 18, 2020
    Posts:
    4
    Logré resolverlo de otra forma, pero gracias por la info tal vez me sirva para otra cosa que quiero implementar.