#Clean Scalable "Provide all targets in an area" System?

1 messages · Page 1 of 1 (latest)

granite thunder
#

Hello, I'm making this post because I'm a bit stumped right now. I have this what I thought was a simple scenario: My player needs a reference to all shootable targets in an area around himself. This is used by my aim assist system to select the aim target. Now what I had first way just a list of all targets that are part of the fight and I literally loop over all of them each frame and make a few checks. Beginning with the distance to the player. This works for few Targets pretty well, but I thought it would be cleaner to only check Targets close enough in the first place, instead of all.

Now I was thinking of using OnTriggerEnter and Exit Methods to cache a list of all Targets inside the AimAssistRange. But this leaves me with some problems. Disabled (Pooled) or destroyed or even Enabled enemies won't cause the OnTriggerExit Method to fire. So I've been thinking of many different ways to "patch this hole up" but they all seem annoying in some way. Like I don't want my targets to have a reference to the aim assist of the player, so they can register themselves. But I also don't want a list of all targets in the aim assist, so it can subscribe to all targets' OnDisable and OnEnable events.

I feel like there must be another clean way to handle this. I would appreciate any tips on this.

restive valley
#

You could even just use a physics query directly to get whats in range every frame

nova pasture
#

I sometimes use events for notifying a system like this when an object is destroyed

#

You can subscribe to the death event for an object when it enters your range

granite thunder
#

That's true but that won't fix the problem of respawning targets from a pool. Enabling targets inside the collider wont trigger OnTriggerEnter as far as i know. So I would have to subscribe to every targets OnEnable too. And I want to avoid the player needing to know every target that could exist. I just went for a very frequent OverlapSphere for now. I thought it would not be performant enough, but it should be fine for this game. I just thought there must be some pattern people use for this that is very performant and clean.