Search Unity

problem with trigger check unity

Discussion in 'Scripting' started by svyathor, Jan 23, 2020.

  1. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    21
    Hello I'm making a 2D game. I have an enemy that has two trigger colliders on each side. I need to make sure that if the Collider is in the trigger, it appears (Debug.Log("right or left will be written here")) and
    Code (CSharp):
    1. //colliders
    2.     public Collider2D TopRig;
    3.     public Collider2D TopLef;
    4.  
    5.     private void OnTriggerStay2D(Collider2D collision)
    6.     {
    7.         if (TopLef)
    8.         {        
    9.             Debug.Log("TopLef");
    10.         }
    11.  
    12.         if (TopRig)
    13.         {
    14.             Debug.Log("TopRig");
    15.         }
    16.     }
    I wrote this script, but when enemy contact any of the trigger colliders, TopReg and topLeft appear in the console
     
  2. ServantOMallard

    ServantOMallard

    Joined:
    Aug 17, 2018
    Posts:
    2
    Hi Svyathor, you need to compare your variables against the collision:

    Code (CSharp):
    1. if (collision == TopLef)
    and

    Code (CSharp):
    1. if (collision == TopRig)
     
    svyathor likes this.
  3. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    21
    Thanks but it doesn't help me
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,025
    why not? You Need to better describe what is happening, what you expected to happen and what you want to happen.
     
    svyathor likes this.
  5. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    21
    I have enemy, I want to make him move. So I made 2 trigger colliders on the sides. I want to check which trigger Collider was affected to rotate enemy.
     
  6. ServantOMallard

    ServantOMallard

    Joined:
    Aug 17, 2018
    Posts:
    2
    How is your enemy gameobject set up? Are both the trigger colliders on the 1 parent gameobject or are they each on separate child gameobjects?
     
    svyathor likes this.
  7. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,025
    it would really help if you told us what you are observing now that you changed the code.

    Have you implemented ServantoMallad's correction where you check to see which collider was returned? If so, which of the two if conditions (TL or TR) fires?

    Also, it ,ay help if you inserted the following line into OnTriggerEnter:
    Code (CSharp):
    1. Debug.Log("object that triggered: " + collision.gameObject.name);
    and then see if the returned collider object is the one that you expected.
     
    svyathor likes this.
  8. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    2,161
    OnTriggerStays reports other colliders that come into those trigger bounds.

    So checking whether its TopRig or TopLeft makes zero sense, it will not work that way, as it is triggered by different colliders.

    To do what you want - calculate direction (collider.transform.position - transform.position) to the reported collider from your position and check against it (e.g. by using .Dot).
     
    svyathor likes this.
  9. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    21
    trigger colliders are children
     
  10. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    21
    after his advice, nothing appears in the console
     
  11. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,025
    It's what Vergil pointed out: neither collider will be TL or TR if they are also the trigger. Insert the line that reports which collider was returned, and you'll find that it is the collder that intersected your trigger, not the trigger itself. Your code will not work as it is.
     
    svyathor likes this.
  12. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,025
    In that case, none of your Triggers were triggered.
     
    svyathor likes this.
  13. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    21
    sorry I'm a beginner so I don't understand much, could you write a small example. I want the trigger to react to the Collider crossing. My code works and outputs tag correctly when crossing the Collider, but two trigger colliders are triggered at once and two lines of TopRig and TopLef are output. After writing the if(collision==Topleft) {....} stopped output (Debug.Log). I think the problem is with incorrectly calling the trigger Collider
     
  14. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,025
    When you are using a Trigger, and a collider is moved into the trigger, the script that is attached to the game object that contains the Trigger is examined if it has an OnTriggerEnter (or in your case: OnTriggerStay) method, and that method is invoked. When invoked, OnTriggerStay is passed the OTHER collider that has entered the Trigger, not which Trigger fired.

    You, however, want to find out which Trigger has been triggered. That will not not work this way.

    Initially, when you code was
    Code (CSharp):
    1. if (TopLeft) {}
    although that code was legal, it did not do what you thought it did. What you wrote is automatically expanded to

    Code (CSharp):
    1. if (TopLeft != null) {}
    which in your case will always return true since you have connected a collider to your public collider variable.

    Currently, your entire logic is wrong. A simple way is to attach a separate script to each collider, and when the collider is triggered, do the appropriate Action for only that collider.
     
    svyathor likes this.
  15. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    21
    Thanks a lot I figured out what to do
     
unityunity