Find nearest enemy object in radius
Hi all, I am trying to create RTS. What is the best and less costly (fps) way to find nearest UNITY object (with tag "Enemy") in some radius . Physics.OverlapSphere, Circle Collider 2d or compare all object positions every frame (I think this way is not good when i have many units ) ? Is there a better way ? (Sorry, I forgot to mention that the game is 2d)
Hi, Unity Answers is not for How-To-Gameplay questions. The moderator who published this one should know that.
Please take a look at the FAQ to check what questions to post on UA.
Have a nice day.
Sorry, but i read FAQ, and maybe i give not enough information, but non off-topic.
This question was about implementing a game mechanic not about playing a game. This shouldn't have been closed.
Of course it is not about playing a game. ^^
I can't find the specific line in the FAQ or User Guide or moderator guidelines at the moment, maybe it got edited since I last saw it.
But regardless, how-to questions like these are offtopic because the are subjective and open for discussion. Because there are many ways to achieve the wanted result. The "what is the best way to" Q's are prone to having extensive discussions because "what is the best" is a subjective view, different from person to person. Exactly what is not appropriate on UA. And that's in the FAQ.
Correct me if I'm wrong please.
PS: Also this would certainly qualify as a duplicate question:
https://www.google.com/?gws_rd=ssl#safe=off&q=unity+how+to+check+for+enemies+in+radius
PPS: Let's keep it open for some time, maybe someone can come up with an even better solution.
Answer by cjdev · Jan 20, 2016 at 12:25 AM
The fastest way would be to compare the square of the distance between them to save on the time that the square root function costs. You can use the built-in properties for this:
// Compare between all nearby enemies to find the nearest:
(transform.position - enemy.transform.position).sqrMagnitude
Hi, don't you think it's your responsibility as well to keep the forum organized? ;) You shouldn't encourage users to post off-topic questions by validating them with an answer.
How is it off-topic to ask about implementing a specific game mechanic while including a method already tried? You know RTS means Real-Time Strategy game right?
Answer by teuba · Jan 20, 2016 at 08:17 AM
You could use Vector3.Distance() to find enemy in your radius.
// The advantage is, no need of physics engine
if(Input.GetMouseButtonDown(0))
{
foreach(ScriptOnYourEnemies e in FindObjectsOfType< ScriptOnYourEnemies>())
{
if(Vector3.Distance(e.transform.position, transform.position) < radiusYouWant)
{
// An enemy is in your radius
e.GetHurt(); // Hit the enemy for example
}
}
}
Your answer
Follow this Question
Related Questions
I need help with AI,Force not working 0 Answers
Animations not playing correctly for FPS enemy AI 0 Answers
Unity job is failed error. 0 Answers
Cant click on GamObject 1 Answer
Problem with Unity updating player prefab over prefab instance 0 Answers