I’m working on a project for android on Unity 2021 and as you may know, i’m a noob at code, anyway this is my case:
I have some cubes in random positions on a plane, then i have a sphere in the middle, i need that sphere to detect the second closest cube and move to that position, not on an update function but after an event something like:
if (bool = true)
invoke detectSecondClosestCube
I asked ChatGPT for a solution and it replied this:
“One approach you could take is to use the Physics.OverlapSphereNonAlloc method to get an array of colliders that overlap with a sphere centered on the position of the sphere. Then, you can iterate through the colliders in the array to find the second closest cube.”
I need a solution without creating an array and filling it manually because the game will have too many “cubes” i need the script to detect the second closest cube in runtime, i can try the ChatGPT solution but this is an android game and i’m not sure if that solution is good for a mobile device, also, ChatGPT’s solutions uses the update function for everything, also, i would have to create very large colliders.
So, Is there a better way to do it? or ChatGPT’s solutions is THE wat to do it?
You are not going to keep track of a collection of objects, without a collection (list or array).
So yes, you need some kind of list or array probably since you need to iterate over all cubes to order them by distance.
You can also add them into a list when you spawn the cubes, so you don’t have to use overlapSphere everything. Or use look for the cubes by tag (filling the list when spawning is better however)
Im not sure if it sorts it by distance tho, if it does youd just have to get the second entry of that array. You would however need to choose a large enough radius such that you get at least two cubes in the array. And if you got that information already, you could probably create a smarter solution.
To suggest anything more specific we’d need some additional information. As suggested by others: how many cubes are we actually talking about and how often does this check happen? Also, for a bit of context, what are you actually doing, why, and what is the goal / game here?
If it’s something like 10 or 20 cubes you can completely disregard performance, even if the check is performed once per frame. If you need to perform it multiple times per frame you can likely cache the result, effectively reducing it to one time per frame. If it’s hundreds or thousands of cubes then that count alone may or may not pose a problem in the final product, completely unrelated to the mechanism implemented here. So really, it depends.
If the distance is always required relative to that one point (your sphere), then you could also just manually handle a list of cubes, even tho you said you dont want that. The question would be: why? It would likely be a good approach. When you instantiate a new cube, add it to your list. When you destroy a cube remove it from that list.
You can now either sort the list when you need to, or maintain it in a way so it always stays sorted, by having each action that would change the order (new cube / deleted cube / sphere moved / cubes moved) adjust the list order accordingly.
@spiney199@DevDunk@Yoreki thanks
Yeah, i was thinking more deeply in my problem, and i think i’ll do it simple.
The sphere will detect the closest cube and if the bool from that cube is “full” then the sphere will look for another closest cube and so on.
More context:
The Sphere is an NPC, i’ll have multiple NPC because those are enemies, lets call them “enemies”, the cubes are random points on the map, lets call them “spots”. The enemies will chase the player, but, when the player hides, the enemies will go to those spots, so i guess i’ll need the spots to have a bool, when a enemy is on that spot, that spot becames “full” so the enemies will need to find another spot that is not full and so on. So i guess i’ll still working with that Physics.OverlapSphere.
Now, that being said, would that process on multiple enemies (between 5 - 15) be too much for a mobile?
BTW: do i need to change the title for this thread?