Search Unity

OnTriggerEnter won't run unless trigger is moving

Discussion in 'Scripting' started by Denisowator, Jan 16, 2019.

  1. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    I have two objects:
    • Object A - Has a 2D trigger collider, a 2D Rigidbody, and a script.
    • Object B - Has a 2D collider, a 2D Rigidbody, and a script.
    Object A's collider starts out disabled. The script enables it on mouse click, then disables it after a certain time (0.1 seconds).

    In Object B's script is an "OnTriggerEnter2D".

    When I first click and enable Object A's trigger collider, Object B's code runs fine, but if I keep clicking without moving Object A, the code only executes once.

    It executes every time the collider is re-enabled, but only whenever I'm moving Object A.

    What's weird is when moving Object A, I don't have to make it exit Object B's collider and then re-enter it. It's enough if I just click the mouse while Object A is in motion.

    Originally I thought the issue was that even though I was disabling and re-enabling the trigger collider, it always counted as already being in the other collider, but apparently that's not the case.
     
    Last edited: Jan 16, 2019
  2. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Bump. Still can't figure this out.
     
  3. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    452
    OntriggerEnter only trigger Once. So it is correct.

    What is your purpose?
     
  4. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    I want certain objects to run their code when entering a trigger collider. But I only want that to be possible when I click LMB (left mouse button). So I make it (clicking LMB) enable the trigger collider, then after 0.1 seconds, disable it.

    Like I said, the code on those objects does run (therefore OnTriggerEnter is triggering) whenever I click LMB, but it only does so if the trigger collider is actively moving as its being enabled, or if I take the trigger collider away from the objects, and then move it back.

    So if neither the trigger collider nor the target objects are moving, and I keep activating the trigger collider, the code on those objects only runs the first time the trigger collider is enabled.
     
  5. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    452
    So you need ontriggerstay, it call every fixedupdate rather than once
     
  6. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Tried that.

    When using OnTriggerStay, the same thing happens.

    Even when I have OnTriggerEnter alongside OnTriggerStay, and have them execute the same code.
     
  7. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    452
    Do you do the checking by turn on, off the collider? You should check mouse only on ontriggerstay.
     
  8. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Yes I'm checking it by turning the collider on and off. I've tried using OnTriggerEnter, then OnTriggerStay, and also tried both at once. Each one gives the same results. Right now I just have OnTriggerStay in the script.
     
  9. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    452
    Dont turn onoff collider.

    Just use ontriggerstay and mouse down click check inside ontriggerstay. You also dont need ontriggerenter
     
  10. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Ok, I’ll try that.
     
  11. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You may need to call RigidBody2D.WakeUp() when you turn the collider back on. I haven't checked this, but your description of what's happening makes it sound like the rigidbody is still asleep.
     
  12. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Agree with that.

    @OP Also, with regards to the other posts recommending OnTriggerStay, it's not needed. Additionally, Input should not be checked anywhere in the physics cycle.
     
    Denisowator likes this.
  13. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Waking up the Rigidbody worked. :)

    Also thanks for the tip Suddoha.