Simultaneous OnTriggerEnter events

I’m relatively new and making a simple zombie game and at the moment I want the zombie to walk up to a doorway that’s been boarded up, stop, play an attack animation, damage/destroy the board, and then when all the boards are gone, continue moving. Everything works great when there’s just one board, but when there are 2 board the zombie will occasionally enter the triggers for both boards at the same time. So it will play the attack animation once but destroy both boards. Is there any way to “focus” on the triggers one at a time? I’m at a loss on how to approach and handle this.

Generally this sort of thing is solved by preventing the destruction of a second board even beginning if there is one already in progress.

Usually this is accomplished by setting a flag when you begin the attack, then clearing it when the attack is done.

That flag would cause the normal trigger to attack to be inhibited or deferred.

You can defer the second and subsequent board destructions in a few possible ways:

  • just don’t do it (this requires you to re-start it even if it’s already in the trigger)

  • put the second one in a queue of things to destroy

Remember that even if they happen in the same frame, no two calls happen “simultaneously…” Unity does one thing at a time.

1 Like

Dumb question, but when you say flag, do you mean anything that would prevent it from triggering the second one, like temporarily disabling the collider, or is there something in Unity called flags that I should look into?

Not a dumb question at all!

When I say flag I mean broadly, “some type of variable to tell you the state of something.”

The simplest flag is a boolean, such as:

bool chewingBoard = false;

You would do 3 things with that flag:

  • set it true when you start chomping on a board

  • set it false when you’re done chomping on the board

  • check it before you begin chomping on a board to make sure you (the zombie) are not already munching.

This can solve a LOT of issues such as the one you list above. But be sure that there is NO WAY that the chewingBoard can be left in an inappropriate state, eg., fail to clear it, fail to set it, etc. You just need to make sure your logic is sound regarding the state of that flag.

1 Like

I added a flag like you suggested and it works perfectly! Thanks so much!

1 Like