3
\$\begingroup\$

So I am attempting to create a zone implementation where if the amount of units with a certain tag exceeds the number of units with another tag an objective will be captured standing in the zone will capture an objective.

However, the problem occurs when counting the units and what the most optimal way of doing it is which is what I hope you guys can help me with! (Also if you have a better way of implementing it then I am all ears).

So in my implementation, I have added a game object with a box collider which is set as a trigger collider

my idea was that as soon as a unit enters or leaves the script will update the count of users with a certain tag.

Should the count of one tag type be greater than the other the victory/capture condition will begin.

This is an ok solution however it has a major problem. Should one of the NPC's inside of the box collider die the script will never detect it as the body has never left the box.

So how do i overcome this problem in a sensable way?

\$\endgroup\$
  • \$\begingroup\$ Have you verified that your counts are not updating correctly? My recollection was that destroying or deactivating an object with a collider also fired OnTriggerExit messages as needed. \$\endgroup\$ – DMGregory Jul 5 at 20:03
  • \$\begingroup\$ @DMGregory The NPC's that i use they have to lie down dead which means they are not actually destroyed :S \$\endgroup\$ – Marc Rasmussen Jul 5 at 20:57
  • \$\begingroup\$ So naturally you decrement the count in the Die() routine, right? Where did you run into trouble making this work? \$\endgroup\$ – DMGregory Jul 5 at 21:01
0
\$\begingroup\$

I'd say the simplest way to handle this is to add to each agent an extra collider for counting, on a dedicated "Counting" layer. You can give it a simple shape, and size it independent of the colliders you use for hit detection, which can help with fairness. (For instance, you might not want a character with big wings to be able to hold a control point from further away than a small compact character can)

You can set up your physics layers so that your victory zones detect collisions/overlaps only with these counting colliders, and vice versa, so you don't have to process & filter uninteresting trigger messages.

Now, when an agent dies, set its counting collider's .enabled property to false. This will fire an OnTriggerExit on its containing victory zone just the same as if it had left the zone or been destroyed. And as a bonus, you can always set it back to true if you resurrect the agent or recycle it via an object pool.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.