Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Jason Hamilton · Aug 25, 2010 at 10:07 AM · javascriptgameobjectenemy

get closest enemy

hey, how do I get a list of all the gameObjects in my scene with the name 'enemy' and then which one is the closest to the object with this script attached to it. thanks

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Bampf · Aug 25, 2010 at 11:31 AM

Rather than find ones with the name Enemy on them, you should either search for objects with a particular script on them, or for objects with a particular tag on them.

I recommend tags, because it's probably much faster, and an example of finding the closest enemy is actually given in the manual entry for FindGameObjectsWithTag. The function you want is FindClosestEnemy. A minor change I'd make: in the example, I see they are calculating distance with two lines of code:

var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude; 

This can be replaced with one line:

var curDistance = Vector3.Distance(go.transform.position - position);

If you need to find the closest enemy often, an alternative might be to put a big spherical collider (set to be a trigger) on the object. Then (assuming the enemies have colliders on them) you will get notified as soon as an enemy moves into range.

Comment
Add comment · Show 3 · 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 Jason Hamilton · Aug 25, 2010 at 08:52 PM 0
Share

I'm trying to make a tower defense game, so how would I do the collider trigger one?

avatar image Bampf · Aug 26, 2010 at 11:11 AM 0
Share

Well the manual has info on Triggers, and there are other answers posted about collision detection in general. It wouldn't be hard to detect when the first enemy moves into range, using triggers. However, once that enemy is dead the tower needs to find another enemy, typically the one closest to the player's base. So you'd probably be better off using tags for that case, not triggers.

avatar image rhys_vdw · Sep 19, 2012 at 09:30 AM 0
Share

I think you mean "Vector3.Distance(go.transform.position, position)". Actually using the sqr$$anonymous$$agnitude is a better choice because calculating the distance squared less computationally expensive than calculating the distance. (It has one less square root.)

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

1 Person is following this question.

avatar image

Related Questions

How to destroy object forever? when i go to another scene and back to first, i want to that object stay destroyed! , Please !!!!  1 Answer

Randomizate bullets help 2 Answers

I need some health with my health/enemy script 0 Answers

Restart this script 3 Answers

AI controller script, Enemy movement issues 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