• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Darkmiay · Jan 19, 2016 at 09:19 PM · c#aiunity 2dradiusfinding

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)

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Le-Pampelmuse · Jan 20, 2016 at 08:49 AM 1
Share

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.

avatar image Darkmiay Le-Pampelmuse · Jan 20, 2016 at 01:17 PM 0
Share

Sorry, but i read FAQ, and maybe i give not enough information, but non off-topic.

avatar image cjdev · Jan 20, 2016 at 08:52 AM 0
Share

This question was about implementing a game mechanic not about playing a game. This shouldn't have been closed.

avatar image Le-Pampelmuse cjdev · Jan 21, 2016 at 04:40 PM 1
Share

@Darkmiay @cjdev

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Le-Pampelmuse · Jan 20, 2016 at 08:51 AM 1
Share

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.

avatar image cjdev · Jan 20, 2016 at 08:59 AM 0
Share

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?

avatar image
0

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
         }
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity job is failed error. 0 Answers

Animations not playing correctly for FPS enemy AI 0 Answers

unity doesn't show me any errors but my character doesn't jump. 0 Answers

Rotate (facing mouse position) a Line Renderer used as Bezier curve 0 Answers

Why won't the coin disappear when the player collides with it? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges