Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Understanding OnTriggerStay2D and 2D Triggers

Discussion in 'Scripting' started by HaydarLion, Aug 16, 2016.

  1. HaydarLion

    HaydarLion

    Joined:
    Sep 17, 2014
    Posts:
    62
    Hey all,

    I am trying to use triggers to detect nearby objects. However, when two objects have triggers, OnTriggerStay2D will be called when the first object enters the 2nd's trigger (the 2nd object has a larger trigger), and not wait until the 2nd object enters the 1st's trigger. Does this mean that OnTriggerStay2D (or exit, or enter, etc) will be called no only when something enter's an object's trigger, but when that object enters a trigger? And what is a good solution to this problem?

    Thanks.
     
  2. AssembledVS

    AssembledVS

    Joined:
    Feb 23, 2014
    Posts:
    248
    The OnTriggerStay2D that is being called is the one that is relative to the object that the script with the OnTriggerStay2D method is attached to. If both objects have a 2D trigger collider component and attached scripts with the OnTriggerStay2D method, both methods will execute at the same time. I don't think that I really understand the problem, but it just seems like you may be a little confused as to how trigger colliders and scripts work in general.
     
  3. HaydarLion

    HaydarLion

    Joined:
    Sep 17, 2014
    Posts:
    62
    Ok then, how do u recommended I fix this? the 2nd object is looking for the player, and the 1st is looking for the 2nd. The 2nd has a significantly larger trigger box, so when the 1st object enters it, it's OnTriggerStay2D is called, even tho nothing entered it's own trigger. At this point, I don't think using trigger boxes is the best way to detect other objects in the scene. If there's no obvious fix to this, can you recommend other method of nearby object detection?
     
  4. AssembledVS

    AssembledVS

    Joined:
    Feb 23, 2014
    Posts:
    248
    What do you mean by "even though nothing entered it's own trigger"? The detection happens only if a trigger collides with it, so it has to enter the trigger in order for OnTriggerEnter2D to execute.
     
  5. HaydarLion

    HaydarLion

    Joined:
    Sep 17, 2014
    Posts:
    62
    So do you mean that a trigger entering that trigger would cause OnTriggerEnter2D to be called? Even if that was the case, the Physics2D settings say that the layers these triggers are on wont collider, so I don't see how that could be the case. I think it might be being called when Object 1 enter's Object 2's trigger, but object 2 (or anything else) hasnt entered object 1's trigger, which is what i'm waiting for.
     
  6. AssembledVS

    AssembledVS

    Joined:
    Feb 23, 2014
    Posts:
    248
    Yes, the collision with the other trigger causes the object's attached script's OnTriggerEnter2D method to be called. If the other object has a script with an OnTriggerEnter2D method, it will also be called, since the other object has a trigger collider as well.

    When you say "layers," are you referring to the collision layer? If your objects are on different collision layers and you disabled collision between these layers, then collision will not occur. What collision layers did you give to your objects?

    It is impossible for object A's trigger collider to enter object B's trigger collider and not have object B's trigger collider simultaneously enter object A's trigger collider. This is what a collision is: an event of intersection between two bodies. You can't touch an apple and not have the apple touch you at the same time.

    Maybe you're trying to figure out how to have object A do something when it collides with object B?

    Perhaps you can post a screenshot of your scene view screen, with the objects' trigger colliders visible?
     
  7. HaydarLion

    HaydarLion

    Joined:
    Sep 17, 2014
    Posts:
    62
    Thanks, your answer above solved my problem.
     
    AssembledVS likes this.