Search Unity

Question OnTriggerEnter: determining which 2 triggers overlapped?

Discussion in 'Scripting' started by FeastSC2, Jun 5, 2020.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    OnTriggerEnter provides the trigger you enter, but does not provide which trigger is entered.

    How to know which trigger is entered? (As cheap as possible)
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The object the script is being run on (e.g. this.gameObject) is the trigger that entered.
     
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    A gameObject with a rigidbody can implement the OnTriggerEnter methods etc... however a rigidbody can have multiple triggers. I would like to know which one of those triggers got entered.
     
  4. DejaMooGames

    DejaMooGames

    Joined:
    Apr 1, 2019
    Posts:
    108
    I don't believe you can get which trigger was entered if you have multiple trigger colliders on the same GameObject. If you split your trigger colliders onto different child GameObjects and have each of them handle their own OnTriggerEnter calls you will be able to see the specific triggers being entered.
     
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    There can only be 1 rigidbody per parent hierarchy.
    In my tests, I saw that OnTriggerEnter doesn't get called on the children of that rigidbody. Am I incorrect?
     
  6. DejaMooGames

    DejaMooGames

    Joined:
    Apr 1, 2019
    Posts:
    108
    You don't need a rigidbody on the gameobject with the trigger colliders if there is a rigidbody on the other gameobject.