Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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#unity 2dairadiusfinding

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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges