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

OnTriggerEnter2D doesn't work if i re-enable it.

Discussion in '2D' started by maxwad, Jun 25, 2022.

  1. maxwad

    maxwad

    Joined:
    Apr 15, 2021
    Posts:
    5
    Sorry, i saw a few threads with my problem, but there are no solution. So...

    I have player. Player beats enemies (colliders) by sword. Sword is simple sprite with collider(trigger). When i enable gameObject "Sword" it calls onTriggerEnter2D and enemy "under" Sword get damage. Then i disable my Sword and eneble in some "attackTime". And it works, but only if my character or enemies move. And it doesn't if they all are still.

    I tryed to disable Collider Component after each attack and eneble it before next. Result is the same.
    And i noticed that problem exists only if one out of pair is real collider, not trigger. If both objects are trigger all works well.

    Can somebody help me?
    P.S. Sorry for my English.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Steps to success:

    - leave all triggers enabled at all times

    - keep track of either enter, exit, or stay, as appropriate

    - inspect your own internal logic to decide if you want to ignore or process these events.
     
  3. maxwad

    maxwad

    Joined:
    Apr 15, 2021
    Posts:
    5
    It's hard to explain, but i can't do this. Thank you any way.)

    Maybe someone else will suggest another solution?
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    I dont think you fully understand Kurt's response. If you cannot enable all the triggers at all times (for whatever reason), you should track them. Throw in a Debug.Log() statement in each OnTriggerEnter2D, Stay2D, Exit2D and see if those are getting called when they should be.

    With the Debug.Logs in place, you need to test every scenario. Have the player and object standing still, both moving, one moving, etc. Start disabling/enabling colliders in each scenario and see what is happening. The debug.logs will really help you. Use them everywhere to see what is going on.
     
  5. maxwad

    maxwad

    Joined:
    Apr 15, 2021
    Posts:
    5
    That's not the point at all. I ALREADY know how it works. I described it above and it's not what I need. When enabled again, the trigger fires only if one of the objects has changed position.

    Kurt gave me the option of not turning off the trigger at all. This is an acceptable option, but it will greatly complicate my code. I'll have to replace the trigger runner with a triggerStay, and give my weapon damage a timer inside the triggerStay, and toggle my weapon sprite on/off (simulating disabling all weapons). And that's okay, but this system is completely inappropriate when I have a different type of weapon that I can't keep on all the time.

    I solved my problem by not turning the sword on and off, but creating and destroying it. That. the weapon triggerEnter is new each time. It works. Yes, it may be wrong in terms of performance, but in my project the issue of lack of resources is not worth it.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    I wrote all the 2D physics code for Unity and I don't understand what you're seeing here. When you disable a physics collider, all its underlying physics details are destroyed. When you enable it, they are recreated. During the next simulation step, all contacts for it will be calculated and reported. It won't need to "move". If you disable a collider, it has no state so when it's enabled and there's a contact, it'll report triggerenter/collisionenter.

    Your description isn't actionable as it's pretty vague TBH. If you have a simple reproduction project of the problem then I would suggest hosting it so I can take a look. Alternately, DM me on these forums with your email and I'll set-up a private workspace for you to do that.
     
  7. maxwad

    maxwad

    Joined:
    Apr 15, 2021
    Posts:
    5
    I have to apologize. I've try to repeat this situation with re-enebling Trigger instead Instantiate - and some miracle happend: now all works as you say. Of course there was some mistace in my code or work's logic. (But i still don't know where exactly i made a mistake)

    Thanks a lot.)