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
2
Question by PrimeDerektive · Dec 13, 2010 at 05:15 AM · arraydistancefindgameobjectswithtag

Check distances of multiple objects, and do something if all outside of range

I've been trying to wrap my head around this for a bit now, and I can't quite figure out out. I'm trying to find the distance between all objects tagged Enemy and my player, and do something if all those Enemy objects are all outside a certain distance... This is as far as I've gotten:

var enemies = GameObject.FindGameObjectsWithTag ("Enemy");
        for (var enemy in enemies){
            var distanceToPlayer = Vector3.Distance(transform.position, enemy.transform.position);
            if(distanceToPlayer >= effectiveDistance)
                //this enemy is outside of the effective range;
        }

However, this only determines if each individual enemy is outside of the effective distance... I'm not entirely sure how to go about making it so I can figure out if ALL the enemy objects are outside of the effective range.

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

2 Replies

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

Answer by Jesse Anders · Dec 13, 2010 at 05:37 AM

One way would be to use a Boolean 'enemy found in range' variable. Initialize it to false before the loop, then, if distanceToPlayer <= effectiveDistance for any enemy, set the variable to true. After the loop has executed, the value of the variable will tell you whether any enemy is in range of the player. (If all you're interested in is whether any enemy is within range of the player, you can terminate the loop at that point. If the test is performed in its own function, you can dispense with the Boolean variable and simply return 'true'.)

An optimization you can apply is to check the squared distance rather than the distance. Also, if the 'enemy' objects have colliders attached, you might be able to use the physics system's 'sphere query' function instead.

[Edit: Providing some additional clarification in case you're just not understanding my answer.]

In your original post, you stated that you wanted to determine if all enemies are outside of a specified range relative to the player. The solution I described above determines if any enemy is inside a specified range relative to the player. This is equivalent. It's like if you handed me a bunch of blocks and asked me to determine whether they're all red. I start looking through them one by one, and the moment I pick one up that's not red, I say, 'Done - they're not all red'. In other words, the existence of just one non-red block is sufficient to prove that not all the blocks are red. Similarly, the existence of one enemy that's in range is sufficient to prove that not all the enemies are out of range.

Does that make sense now? As for your comment below, the question you stated there is the opposite of the question stated in your original post, so perhaps you could clarify (via a comment or by editing your question) what it is that you're really wanting to know.

Comment
Add comment · Show 5 · 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 PrimeDerektive · Dec 13, 2010 at 05:45 AM 0
Share

I don't want to find out if any one enemy is in that range though... i want to find out if ALL the enemies are within that range.

avatar image Jesse Anders · Dec 13, 2010 at 10:15 AM 0
Share

Ouch - did you downvote my answer? Your original post says, 'do something if all outside of range', and 'so I can figure out if ALL the enemy objects are outside of the effective range'. This strongly suggests that you want to know if all of the enemies are outside a specified range, which is the question that I answered. Now you're saying, 'i want to find out if ALL the enemies are within that range', which is the opposite of that. (If upon reflection you decide my answer wasn't as off base as you thought, I'd appreciate it if you voted it back up - you're messing up my record! ;)

avatar image PrimeDerektive · Dec 13, 2010 at 03:16 PM 0
Share

Hah sorry man I didn't mean to do that. I can't seem to ever get people to come back and respond to comments because I don't think UnityAnswers notifies people of comments, so I thought I'd be able to get you to come back if I downvoted you, and then I would upvote you :)

avatar image PrimeDerektive · Dec 13, 2010 at 03:18 PM 0
Share

I totally get it now, I am retarded. I was thinking I was going to have to have like an array of booleans, but this makes so much more sense.

avatar image Jesse Anders · Dec 13, 2010 at 03:57 PM 0
Share

Lol, ok - glad you got it figured out :)

avatar image
0

Answer by Badger · Apr 10, 2011 at 06:14 PM

Hi there Jesse,

Would you care to elaborate on the sphere query function? Any example scripts on that matter?

thx B

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

No one has followed this question yet.

Related Questions

geting the closest object from a array 2 Answers

store GameObject.FindGameObjectsWithTag in an array 2 Answers

Get distance from all objects in an Collider[] and compare 1 Answer

Find gameobject furthest from player along z axis 0 Answers

How to access the return value of GameObject.FindGameObjectsWithTag() ? 2 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