Efficient way of detecting if objects are within an area?

In one of my projects I’ve been messing with a few different ways of detecting if an Object is within a set radius every few seconds and then depositing resources to the ones that are ~

My issue is, is that i’m currently doing a lot of calculations and if a player were to hit 50 ish units the game would start to lag on mobile platforms, being that i’m hoping to release this on mobile this is somewhat of an issue

What I’m asking is if there’s an efficient way for one central object to do a check say every minute and distribute a portion of resources to each object? something like this

Timer(){
if (TimerIsExpired)
{
//find objects within radius
//divide currently held resources by number of objects
//give each object x amount of resources
}
}

I think you should add a sphereCollider to a gameobject and it to the rootBone of your character, add a script to the sphereCollider and use TriggerEnter and TriggerExit, if a rigidBody enters there sphere the script should add them in the list and get the distance from each of the rigidBody, use raycast to hit the closest rigidBody if it returns something other than the rigidBody then it is not in the FOV, in that case raycast to the second rigidBody in the list, Once the raycast hits the rigidBody you should notify the player class there is an item of interest in the area and you player should act the way you want it to.

One optimization you can do is using square magnitude rather than actual distance between two objects in your calculations, as this is much faster.

Another important aspect is how you’re finding references to all your checked targets. If you’re using something like FindObjectsOfType, that is not as fast as if you had a pre-cached array of targets that you loop through. One way to build one is to have actors add themselves to a list when they spawn, and remove themselves from it when they despawn.

A third optimization you can do is not trying to check all objects within a single frame, but instead spreading the checking over the span of multiple frames. This way the performance could be exactly the same if you have 10 units or 100 units, but just takes more frames to finish.

Using sphere colliders like @cmyd suggested is probably also a usable approach, as the collision detection has all kinds of optimizations built-in. However if you’re only using that data for this one thing something like once a minute, it also feels pretty wasteful to have all those collision checks running in the background every frame even outside of that.

I would’ve told him more approaches but he didn’t share any background, that being said I could only give him general optimization tips.

1 Like

Thank you for the tips, to give some more insight my current setup does use a triggers and is fairly inefficient in it’s setup, I’ll try your methods and see if I can squeeze some more performance out of it!

1 Like

Since you mentioned you’ve 50ish characters at the same time I want to ask a question, are they all in the screen all times? if so I found the number to be bit high.

I did end up fixing the problem for the most part, Optimized my code and it’s working now and it is possible in the game to have all 50 at once on screen however I’ve created a fog like appearance and anything within the fog or that isn’t currently in view the the camera has its renderer disabled along with any other components that don’t need to be active

1 Like