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
0
Question by finnbennet42 · Jan 07, 2021 at 05:53 AM · liststargeting

How to target multiple enemies (add to list)

Hi! I'm pretty new to Unity and completely new to the forum so apologies if I do something wrong. I'm trying to target a number of enemies with an ability. The number of targets is defined by a scriptable object so it needs to be able to change depending on the ability. I'm trying to do it by adding the enemies to a list when you click them, but I can't get it to work and can't find anything by Googling (I'm at that stage where I don't quite know what to Google). At the moment it seems to add all the enemies as soon as I activate the ability. (It's turn based combat if that makes any difference).

 if (!abilityOne.IsGather)
                                     {
                                     
                                     do
                                     {
                                         targetCD = abilityOne.NumTargets;
                                     }
                                     while (targetCD < 1);    
                                     
 
                                     if (Input.GetMouseButtonDown(0) && targets.Count < abilityOne.NumTargets)
                                     {                                                                                                       
                                         IDamagable target1 = (hit.collider.gameObject.GetComponent<IDamagable>());   
                                         targets.Add(target1);                                                                                                                                             
                                     }                                                                       
 
 
                                     if (targets.Count == abilityOne.NumTargets)
                                     {                                    
                                                                            
                                         foreach (IDamagable target in targets)    
                                         {
                                             int randdam = Random.Range(abilityOne.DamageL + focus, abilityOne.DamageH + 1);
                                             int randheal = Random.Range(abilityOne.HealL + focus, abilityOne.HealH + 1);
                                             Vector3 targetlocation = target.Location;
 
                                             if (randdam > abilityOne.DamageH)
                                             {
                                                 randdam = abilityOne.DamageH;
                                             }
 
                                             if (randheal > abilityOne.HealH)
                                             {
                                                 randheal = abilityOne.HealH;
                                             }  
 
                                             if (target != null && Vector3.Distance(Location, targetlocation) < abilityOne.AbilityRange)
                                                 if(water >= abilityOne.CostWater && actionpoints >= abilityOne.CostAP)
                                                 {
                                                     
                                                     if (abilityOne.IsAttack)
                                                     {
                                                         target.Damage(randdam);                   
                                                         randdam = 0;
                                                     }                                        
 
                                                     if (abilityOne.IsHeal)
                                                     {
                                                         target.Heal(randheal);
                                                     }
                                                     
                                                     if (abilityOne.Knockback == Knockback.Target)
                                                     {
                                                         float force = 500;  
                                                         Vector3 dir = transform.position - target.Location;
                                                         dir = -dir.normalized;
                                                         target.Falling = true;
                                                         target.obj.GetComponent<Rigidbody>().AddForce(dir*force);
                                                     }
 
                                                     
                                                 }
                                             } 
 
                                         targetCD -= 1;
                                      
                                         } 
                                     
                                     if (targetCD == 0)
                                     {
                                         water -= abilityOne.CostWater;
                                         actionpoints -= abilityOne.CostAP;
                                         ability = 0;    
                                     }
                                     
                                     }     
                                 }  

Also if I don't have the do while loop bit it just goes straight to the bottom bit and ends the ability. As you can probably tell I have no real idea what I'm doing. What is it I'm doing wrong? Or is there just a whole better way of doing this that's completely different?

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
0

Answer by ddooms · Jan 07, 2021 at 08:59 AM

It does seem like you're making a mountain out of a mole hill. If you use lists, you can basically pass a regex that only accepts the ones with the ability. There's a library you need to import though, I think it's linq? Anyway, you can use this:

 List<ability> abilities = new List<ability>();
 
 Abilities = FindObjectsOfType().Where((g) => g.getcomponent<Ability>().type == some type);

My syntax is probably off here, but the overall idea is the same. Look up the .Where keyword for lists.

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

111 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 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 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 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 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 avatar image

Related Questions

List within a list problem 1 Answer

In a group of gameobjects, how to have only one object active at a time while disabling the others? 2 Answers

How to modify array values? 1 Answer

Affect every object in array. 1 Answer

How to determine a "cone of influence" used for targeting an object. 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