Search Unity

Monitor in every trigger which objects my main object collides

Discussion in 'Physics' started by v_chs, Jun 27, 2020.

  1. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    I develop a fire propagation system, and I'd like to monitor which objects the fire touches every moment of the game. In the fire object, I attached a box collider, a rigidbody, and also a script in which I want to implement the OnTriggerEnter method.

    In the method below I add the collided objects each time this method is called and I check if the only object in the list is the Plane. If so, the fire stops. If it touches more objects, I want it to move.

    I ran a test, and if the fire touches only the plane I get the correct flag, but if it collides with another object, and pass it by, I cannot detect that now only the Plane is touched. It still has the previous state. I suppose I should implement something in OnTriggerExit, but I haven't figured it out yet. Any ideas?

    Code (CSharp):
    1.     private void OnTriggerEnter(Collider other) {
    2.  
    3.         collided.Add(other.name);
    4.         for (int i = 0; i < collided.Count; i++) {
    5.             print(collided[i]);
    6.         }
    7.  
    8.         if (collided.Contains("Plane") && !collided.Contains("Cube")) {
    9.             print("Stop propagating the fire!");
    10.         }
    11.         else {
    12.             print("Move!");
    13.         }
    14.     }