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 smokk83 · Dec 04, 2019 at 05:12 PM · scripting problemscripting beginnerscriptingbasicstargeting

Attacking mulitple targets simultanously

Hi Pros,

i am a newbie hobby programmer since 6 month and i love it. I tryed right now a Tower def game with a good tutorial and just wanted to add some extra stuff to train my skills. Right now the towers just attack one enemy if he is dead or out of range they target the next.

I want to add a tower that can attack all enemys. I tried it with an new list in wich i add all enemys that enter the collider, and with a foreach loop i wanted to set them as targets. If i read the logs thos works but my damn tower doenst want to attack simultaneously. He attacks the enemys right one after another. Any tipps would be great and dont be to harsh with me, just learning tight now :D thx for help :)

 MonoBehaviour { [SerializeField] private string projectileType;
 
   [SerializeField]
   private int damage;
  
   private Monster target;
  
   private bool canAttack = true;
  
   private float attackTime;
  
   List<Monster> attackList = new List<Monster>();
  
   [SerializeField]
   private Animator animator;
  
   [SerializeField]
   private float attackCooldown;
  
   [SerializeField]
   private float projectileSpeed;
  
   public int MyPrice { get; set; }
  
   public float MyProjectileSpeed { get => projectileSpeed; set => projectileSpeed = value; }
   public Monster MyTarget { get => target; set => target = value; }
   public int MyDamage { get => damage; set => damage = value; }
  
   // Start is called before the first frame update
   void Start()
   {
  
   }
  
   // Update is called once per frame
   void Update()
   {
       SetTarget();
       Attack();
   }
  
    
   public void SetTarget()
   {
       if(target == null && attackList.Count > 0)
       {
           foreach (Monster monster in attackList)
           {
               target = monster;
               Debug.Log(monster);
             
           }
       }
  
   }
  
   public void Attack()
   {
  
       if (target != null && target.IsActive)
       {
           Shoot();
       }
  
       if (target != null && !target.IsAlive || target != null && !target.IsActive)
       {
           target = null;
       }
   }
   private void Shoot()
   {
  
       Projectile projectile = GameManager.MyInstance.MyPool.GetObject(projectileType).GetComponent<Projectile>();
       projectile.transform.position = transform.position;
       projectile.Initialize(this);
  
  
  
   }
  
   public void OnTriggerEnter2D(Collider2D other)
   {
       if (other.tag == "Monster")
       {
  
           attackList.Add(other.GetComponent<Monster>());
           Debug.Log(MyTarget);
       }
   }
  
   public void OnTriggerExit2D(Collider2D other)
   {
       if (other.tag == "Monster")
       {
           attackList.Remove(other.GetComponent<Monster>());
           target = null;
  
       }
   }
  
  
   
 }

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
0
Best Answer

Answer by revolute · Dec 05, 2019 at 01:24 AM

In SetTarget, this line:

    if(target == null && attackList.Count > 0)

prevents you from multi targeting. For the first time that a GameObject is enlisted in the attackList, SetTarget is called and target is set to that object. Until it dies or leaves the trigger, target will not change and only one will be attacked.

Either make the variable target to a list or an array to hold multiple targets and handle them in attack or just call Attack in SetTarget and cleanup the target variable right after.

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 smokk83 · Dec 05, 2019 at 08:48 AM 0
Share

Thx Sir :)

avatar image
0

Answer by Anis1808 · Dec 05, 2019 at 01:46 AM

Your problem occurs on line 49, specifically target = monster; because in your foreach loop, you are overriding your target variable EACH time, so the target will only take the last entry of the loop. In your situation (and I assume this script is for the multi-target tower only), I would add a new function in the GameManager script, which I dont have access right now that takes a list as input and that instantiates a single projectile for each of your Monsters and call it on line 73 of the current script. Since all the code for spawning is in the GameManager, I cannot help you with code so I wish my explanation helps you, otherwise please provide me with the GameManager script so I can give you better explanation.

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 smokk83 · Dec 05, 2019 at 08:48 AM 0
Share

Thx for Help, will look in this solution too. Always good to know more ways:) Thank you so much guys

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

125 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 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

How to implement tokenization of game characters? 1 Answer

Beginner Question: How to get normals from a physics raycast using visual scripting? 0 Answers

weighted inventory system 1 Answer

How to toggle on and off an animation with the same key? 1 Answer

Quick code help! (c#) 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