• 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 mpriess93 · Feb 10, 2020 at 07:17 PM · damagecombatarea-damage

Aoe (Area of effect) only hits one at a time instead of all that have been hit

Hello Everyone, I tried to create a "field of damage" with a Physics.OverlapBox. Tracking of multiple colliders is possible with my code and i can do damage at these. But only one at a time. If one died, the next target gets damaged but i want that all targets within the Physics.OverlapBox gets damaged at the same time.

 [RequireComponent(typeof(CharacterStats))]
 public class PlayerCombat : MonoBehaviour
 {
     bool m_Started;
     public LayerMask m_LayerMask;
     public GameObject Cube;
 
     public float attackSpeed = 1f;
     private float attackCooldown = 0f;
 
     public float attackDelay = .6f;
 
     public event System.Action OnAttack;
 
     CharacterStats myStats;
                     
 
     private void Start()
     {
         myStats = GetComponent<CharacterStats>();
         m_Started = true;
     }
 
     private void Update()
     {
         attackCooldown -= Time.deltaTime;
     }
 
     public void Attack(CharacterStats targetStats)
     {
         if (attackCooldown <= 0f)
         {
             StartCoroutine(DoDamage(targetStats, attackDelay));
 
             if (OnAttack != null)
             {
                 OnAttack();
             }
             attackCooldown = 1f / attackSpeed;
         }
     }
 
     IEnumerator DoDamage(CharacterStats stats, float delay)
     {
         //Use the OverlapBox to detect if there are any other colliders within this box area.
         //Use the GameObject's centre, half the size (as a radius) and rotation. This creates an invisible box around your GameObject.
         Collider[] hitColliders = Physics.OverlapBox(Cube.transform.position, Cube.transform.localScale / 2, Quaternion.identity, m_LayerMask);
         int i = 0;
         //Check when there is a new collider coming into contact with the box
         while (i < hitColliders.Length)
         {
             //Output all of the collider names
             Debug.Log("Hit : " + hitColliders[i].name + i);
             //Increase the number of Colliders in the array
             i++;
 
             yield return new WaitForSeconds(0);
             stats.TakeDamage(myStats.damage.GetValue());
         }
     }
 
     void OnDrawGizmos()
     {
         Gizmos.color = Color.red;
         //Check that it is being run in Play Mode, so it doesn't try to draw this in Editor mode
         if (m_Started)
         //Draw a cube where the OverlapBox is (positioned where your GameObject is as well as a size)
         Gizmos.DrawWireCube(Cube.transform.position, Cube.transform.localScale);
     }
 
 }

And this is my CharactersStats class: public class CharacterStats : MonoBehaviour { public Slider myhealthBar;

     public int maxHealth = 100;
     public int currentHealth { get; private set; }
 
     public Stat damage;
     public Stat armor;
 
     void Awake()
     {
         currentHealth = maxHealth;
     }
 
     void Update()
     {
 
         if (Input.GetKeyDown(KeyCode.T))
         {
             TakeDamage(10);
         }
     }
 
     public void TakeDamage(int damage)
     {
         damage -= armor.GetValue();
         damage = Mathf.Clamp(damage, 0, int.MaxValue);
 
         currentHealth -= damage;
         Debug.Log(transform.name + "takes" + damage + "damage.");
         Debug.Log(transform.name + "at health" + currentHealth);
 
         if (currentHealth <= 0)
         {
             Die();
         }
 
     }
 
     public virtual void Die ()
     {
         // Die in some way
         // This method is meant to be overwritten
         Debug.Log(transform.name + "died.");
 
 
     }
 
 }
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 jkpenner · Feb 11, 2020 at 12:23 AM 0
Share

Does your script output all the correct collider names?

The main thing I see with your script is that you never get the CharacterStats component from the hit colliders. Each time you call DoDamage it calls TakeDamage on the same CharacterStat, once for each collider hit. It looks like you may need something like:

 var hitStats = hitColliders[i].gameObject.GetComponent<CharacterStats>();

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

120 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

Related Questions

continuous damage 1 Answer

Trigger and ennemy damage 2 Answers

Multiplayer do damage 0 Answers

I have a problem inflicting damage to my enemies... It works when using it without an array but as soon as I I do put them in it gives me a "MissingMethodExpection"... Here is the code 0 Answers

Need help implementing a damage reduction 1 Answer


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