Some way to accomplish OnTriggerExit when opposing object is destroyed?

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.)

Something like this…

GameObject enemy;

bool isAttacking;

void Update()
{
  isAttacking = (enemy != null);
    
  if(isAttacking)
  {
      //do the attacking stuff
  }
  else 
  {
     //do movement stuff
  }
}

void OnTriggerEnter(Collider other)
{
  enemy = other.GameObject;
}

Do something like this.

← required fields →
List targets
GameObject target
bool isAttacking

OnTriggerEnter()

  • Check if the game object that has entered is not in the list of targets, if it is not, add it
  • Assign target by whatever mean, closet or whatever.
  • If target is not null isAttacking should be set to true.

OnTriggerStay()

  • Check if the target is not null, keep attacking
  • If target is null, loop through the targets list and assign target a new target to attack.
  • If there are no other targets, isAttacking should be set to false so it can move on

OnTriggerExit()

  • Check if the game object is in the list, if it is remove it
  • Check if the game object that is leaving is the current target, if it is loop through the list of targets to assign a new one if possible.
  • If target is still null isAttacking, should be set to false.

If a target gets destroyed, this is no OnTriggerExit event.

Not a good reason to not check, if you are not null checking what are you doing?

I think you completely missed the point.

You cant check your list for an item, if the item never tells you its left a trigger.

You can enumerate your list looking for nulls, but thats not the same thing.

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.

A good programmer, always expects the unexpected.

Perhaps you don’t need OnTriggerExit if you want to kill him at all costs! :slight_smile:

1 Like

Thats the plan :wink:

/yawn… did you even bother looking at the code I posted… comprehension 101