Search Unity

  1. Click here to receive a gift with your purchase of Unity Pro or Unity Enterprise.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Reenabling disabled gameobject does not call ontriggerenter.

Discussion in 'Physics' started by Yukken, Oct 23, 2019.

  1. Yukken

    Yukken

    Joined:
    Jul 12, 2015
    Posts:
    64
    I am trying to implement hitboxes by toggling trigger colliders on and off. The trigger colliders are attached to a parent rigidbody. This is the code I'm using to detect enemy hitboxes inside the collider.
    Code (CSharp):
    1. public abstract class TriggerTracker2D<T> : MonoBehaviour
    2. {
    3.     public List<T> trackedList = new List<T>();
    4.  
    5.     public Collider2D col;
    6.  
    7.     private void OnEnable()
    8.     {
    9.         clear();
    10.     }
    11.  
    12.     private void OnDisable()
    13.     {
    14.         clear();
    15.     }
    16.  
    17.     public void clear()
    18.     {
    19.         trackedList.Clear();
    20.     }
    21.  
    22.     public abstract void onTrackerAdd(T newlyTrackedObject);
    23.     public abstract void onTrackerRemove(T newlyTrackedObject);
    24.  
    25.     private void OnTriggerEnter2D(Collider2D collision)
    26.     {
    27.         Debug.Log(gameObject.name + " collides with " + collision.gameObject.name);
    28.         T trackedComponent = collision.GetComponent<T>();
    29.         if (trackedComponent != null)
    30.         {
    31.             if (!trackedList.Contains(trackedComponent))
    32.             {
    33.                 Debug.Log(gameObject.name + " to track" + collision.gameObject.name);
    34.                 trackedList.Add(trackedComponent);
    35.                 onTrackerAdd(trackedComponent);
    36.             }
    37.         }
    38.     }
    39.  
    40.  
    41.     private void OnTriggerExit2D(Collider2D collision)
    42.     {
    43.         Debug.Log(gameObject.name + " stops colliding with " + collision.gameObject.name);
    44.         T trackedComponent = collision.GetComponent<T>();
    45.         if (trackedComponent != null)
    46.         {
    47.             Debug.Log(gameObject.name + " stops tracking " + collision.gameObject.name);
    48.             trackedList.Remove(trackedComponent);
    49.             onTrackerRemove(trackedComponent);
    50.         }
    51.     }
    52. }
    Code (CSharp):
    1. public class Hitbox: TriggerTracker2D<Damagable>
    2. {
    3.  
    4.     public DamageInfo damagePerHit = new DamageInfo();
    5.  
    6.     public override void onTrackerAdd(Damagable newlyTrackedObject)
    7.     {
    8.         newlyTrackedObject.takeDamage(damagePerHit);
    9.     }
    10.  
    11.     public override void onTrackerRemove(Damagable newlyTrackedObject)
    12.     {
    13.      }
    14. }
    15.  
    Basically, the hitbox is derived class that hooks on to the onTrackeradd() calls and deals damaged to the object. The abstract class takes care of checking the components.

    I control this script by enabling the gameobject the hitbox(and the corresponding trigger collider) is attached to when I need to start damaging and disable the gameobject when I need to stop.

    The first time I toggle the gameobject on and off, it calls ontriggerenter and exit events properly. BUT every time I try to toggle the hitbox gameobject afterwards, the ontrigger events are NOT called unless I move the character.

    AFAIK ontriggerenter and exit calls should be called every time when a gameobject is turned on/off. So I'm not sure why this is not working. I've used a similar method previously to implement hitboxes and it worked.

    If it matters,I am using rb.moveposition to move my player and it has a kinematic rb attatched. The hitbox gameobject is a child of the player gameobject.
     
unityunity