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 /
avatar image
0
Question by n-carlson1 · Jul 18, 2016 at 07:21 PM · collisiongameobjectmultipletargetattack

How can I target/attack all gameobjects in a certain area?

So it's a little more complex than the title in the grand scheme of it all. I want to have a spell that attacks up to 6 enemies in an area and they can be hit a maximum of 1 times.

So far I had

  bool enemyInBounds = false;
  bool enemyTargeted = false;
  void SpellOne()
  {
  if (enemyInBounds == true)
  {
      for (int i = 0; i < 6; i++)
      {
          //need to make sure they're unique
          // apply damage and shoot projectile
      }
  }

I wasn't sure how to check if they were inside the sphere collider I had on the player besides

  void OnTriggerStay(Collider Other) //might work. not sure if enemyInBounds is assigned to the other object or not
  {
  if (Other.gameObject.tag == "Enemy")
      enemyInBounds = true;
  }
  void OnTriggerExit(Collider Other)
  {
  if (Other.gameObject.tag == "Enemy")
      enemyInBounds = false;
  }

I'm not sure if this is even the correct way to go about it. Any ideas on the logic?

Comment
Add comment · Show 1
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 Masterio · Jul 18, 2016 at 07:50 PM 0
Share

Try this method:

https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html

Alternativly you can iterate over all enemies and calculate the distance between the enemy and the spell position.

Triggers are updated with physics engine so refresh rate is the same.

1 Reply

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

Answer by Charmind · Jul 19, 2016 at 03:22 PM

If i didn't got it wrong You can create list of enemy gameobjects and EnemyScript so:

     List<GameObject> Enemys = new List<GameObject>();
     List<EnemyScript> Temporary = new List<enemyScript>();

Then make script like this:

 Temporary = FindObjectsOfType<EnemyScript>();
 for(int i = 0; i < Temporary.Count; i++) // Finding all Enemy objects and adding them to list
 {
     if(Vector3.Distance(Temporary[i].gameObject.transform.position, transform.position) < spellRange) // check if Enemy is at range
     {
         Enemys.Add(Temporart[i].gameObject);
     }
 }
 
 int selected;
 int Enemycount = Enemys.Count;
 
 for(int i = 0; i<Mathf.Min(6,Enemycount); i++) // selecting random 6 unique enemys also checking if enemy amout is smaller than 6
 {
     selected = Random.Range(0,Enemys.Count);
     Enemys[selected].getComponent<EnemyScript>().spellX(); // send enemy info that he is damaged by your spell
     Enemys.RemoveAt(selected); // remove this enemy so it's unique
 }


Remember to add using.Systems.Collections.Generic. Hope it helps :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Enemy Collision Help 0 Answers

Colliding with multiple objects to destroy other gameObject 4 Answers

Attack multiple targets in the same tag? 1 Answer

Can I play multiple AudioSources from one gameobject? 8 Answers

Physics.OverlapSphere() not working after using pool of objects 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