Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 convictcartel · Jan 12, 2014 at 05:41 AM · distancetagoverlap

Find Objects with Tag in Distance (AOE Spell)

I am aware of (Physics Overlap) but honestly I do not understand it, hours of looking and research I just cant find something that im looking for.. so if anyone could explain this to me please! Greatly appreciated! All I want is when this command is called that any game objects with ("enemy") tag will be affected or any objects with that tag in distance etc. Here is my code, in C##.

 if (animation["footkick"].enabled == true) {
             float distance = Vector3.Distance(target.transform.position, transform.position);
             if(distance < 8) {
                     stinkbreathAttack();
             }
 
         }

 
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 KellyThomas · Jan 12, 2014 at 06:30 AM

Well you could use FindGameObjectsWithTag() to locate all of the enemies.

 if (animation["footkick"].enabled == true) {
     GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
     foreach(GameObject target in enemies) {
         float distance = Vector3.Distance(target.transform.position, transform.position);
         if(distance < 8) {
             // perform attack on target  
         }
     }
 }


Comment
Add comment · Show 6 · 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 convictcartel · Jan 12, 2014 at 11:44 AM 0
Share

Thank you so much, works perfect. Finally!

avatar image pmanalex · Dec 14, 2014 at 12:36 AM 0
Share

Can you include a javascript version of this code?

avatar image Ambassador_of_Infinity · Sep 29, 2018 at 03:03 PM 0
Share

When object with tag is destroyed, the script glitches

avatar image KellyThomas Ambassador_of_Infinity · Sep 30, 2018 at 01:08 AM 0
Share

Are you perfor$$anonymous$$g the foreach iteration over a changing collection? It won't like that!
But by using enemies as a stable intermediary it shouldn't be a problem.
What specifically is going wrong?

avatar image Ambassador_of_Infinity KellyThomas · Oct 01, 2018 at 01:50 PM 0
Share

The programme plays different music when in battle. When distance is higher than limit (not battle) - the not battle music plays (correctly). When distance is lower than limit (battle) - the battle music plays. When enemy is destroyed (from battle to not battle) the music doesn't play (wrong, not battle music should play). I will try to change loops

Show more comments
avatar image
1

Answer by Reid_Taylor · Apr 15, 2020 at 06:15 PM

While #KellyThomas's idea would work; it is not ideal. The method FindObjectsWithTag is a heavy method and is not performant. A more efficient way to accomplish this would be this:

 if (animation["footkick"].enabled == true) 
 {
       Collider[] enemies = Physics.OverlapSphere(transform.position, RANGE);
       foreach (Collider col in enemies) 
       {
             if (col.gameObject.tag == "enemy")
             {
                     Enemy enemy = col.GetComponent<Enemy>();
                     if (enemy != null) 
    .                {
                            // Inflict damage
                     }
             }
       }
 }

Comment
Add comment · Show 1 · 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 u11303418 · Mar 23, 2021 at 08:26 AM 0
Share

@Reid_Taylor Could you please explain the above?

I am trying to implement this to a land trigger, looking for adjacent land triggers around it within a specific range, then opening the closed lands around it, and generating new closed lands around the new open land..

 public class LandTriggerScript : MonoBehaviour
 {
 public bool isLandOpen = false;
 public bool hasNorthLand = false;
 public bool hasEastLand = false;
 public bool hasSouthhLand = false;
 public bool hasWestLand = false;
 public bool hasNorthEastLand = false;
 public bool hasSoutEastLand = false;
 public bool hasSouthWestLand = false;
 public bool hasNorthWest = false;
 
 public GameObject[] adjacentTriggers;    
 
 public void PrepareTriggers()
 {   
    Collider[] enemies = Physics.OverlapSphere(transform.position, 30);
    foreach (Collider col in enemies) 
    {
         if (col.gameObject.tag == "MapTrigger")
         {
             //add game object to the array of gameobjects named adjacentTriggers
         }
    }

    for(a = 0 ; a < adjacentTriggers.length; a++)
    {
        //if the gameobject's x axes is smaller than transform's x value, set the transform's hasWestLand boolean true,
             //as well as the opposite side for the target game object...and so on for all 8 sides
             
    }

 }

   

}

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

22 People are following this question.

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

Related Questions

lookAt nearest object to another object 1 Answer

tag distance script 2 Answers

Updated Question if statement not executing case 1 Answer

Distance from tagged objects problem 1 Answer

How is OverlapSphere used? 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