- Home /
Detecting multiple gameobjects
So, I have a collider on my enemy gameObject. What I want it to do is when any gameObject with a specific Tag enters, it store it in an array.
And then I want to get the enemy to find the closest gameObject that is in the array, set it to it's target and walk towards it and attack bla bla bla. But then if the target gameObject is still in the collider, but another gameObject happens to be closer, then switch the target and chase that one instead. (the whole walking towards and attacking is just so you know the "big picture" of what i'm trying to do, but okay with doing that myself, heh).
I know how to Compare Tag, so if it was only one gameObject I was "attacking" it would be fine. But storing multiple gameObjects that have entered the collider and keep checking if the "target" is the closest one, then if not switch target.
And then to finish it off, if one of the gameObjects leaves the collider, then remove it from the array.
Maybe i'm over thinking this and I don't need an array( or list), and there's a simpler way to do this? I'm not too sure.
Any help or a point in the right direction in doing this would be great.
$$anonymous$$aybe you can work with the spherecast to detect close entities. Then make a simple Vector3.Distance() comparison between all entities returned by the spherecast ?
I would add that when using sphere cast for something like this you shouldn't check every frame (it's expensive). I would put a counter that checks once every time 't', making 't' as large as possible without beco$$anonymous$$g visible in the game.
You have a long list of criteria here that makes this question appear as a write-it-for-me question. Unity Answers addresses a single specific technical problem to help you write your own code. So looking at just finding the closest within a specified distance:
I suggest you use a Physics.OverlapSphere() each frame. This will give you a list of object within a specified distance of your enemy. You then need to run through the list and find the closest one. No need for a collider to solve the problem. In fact, if the number of 'specific Tag' objects is not huge, just use GameObject.FindGameObjectsWithTag() and compare the distance, doing nothing if the closest is not within a threshold.
@ABerlemont, thank you for your input, I did not know of spherecast, I will do some research into it. @robertbu, as you for input, although it is also helpful, you decided to attack my question. I have not asked you to "write it for me", nor have I asked multiple different questions not based on the title. I gave a little background on what I was doing, merely stating what I thought I could try and asking for opinions on whether this was a good way to approach or if there were better ways. So please, get off your high horse.
When I do some more research and find it is what i'm looking for I will update the answer here.
Answer by getyour411 · May 27, 2014 at 11:14 PM
SphereCast/OverlapSphere/iterate over Find...Objects, any of the comments should prove to be a solution, just posting a recap to have an Answer provided.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145833im_/https://answers.unity.com/themes/thub/images/avi.jpg)