I have to objects constantly moving at one another;
–>____<–
Once they collide they start to ‘fight’ –><–
When one of them ‘dies’, I need to call something like OnTriggerExit so that the survivor can keep moving;
_ →
However whenever you destroy an object it doesn’t count as leaving the collision. So how might I check for something like this?
You might use OnTriggerStay to switch from “movement” mode to “fighting” mode. Essentially, you could store the Time.time value in OnTriggerStay, which reflects when you last ran into something. Then in Update or FixedUpdate, look at that stored time value, and if it’s more than some small amount, switch back to “movement” mode.
(Or use a slightly larger amount to make your unit take a little pause to gather its wits before moving forward again — I think that might be cool.)
Exit may not always get triggered but that doesn’t mean you should not null check. For me when it comes to ‘enemy’ game objects I never destroy them, that is a waste of CPU having to Instantiate a new one, I just deactivate them. When this happens, you have to continue to null check for any case. Triggers, keep in mind are flimsy and you should not get into the habit in thinking that they will always trigger correctly. At higher speeds, they may or may not trigger in the proper order so it is a must to always check your cached list.