• 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 KhenaB · May 31, 2017 at 05:56 AM · enemydamagecooldown

Damage enemy cooldown

Hey,

I have a script on my weapon that calls a Damage function on the enemy OnTriggerStay2D. The weapon has a cooldown so that the enemy only gets hit as often as the cooldown.

The problem is that the weapon cannot hit multiple enemies since the first enemy hit will trigger the cooldown. What i want is a cooldown per enemies.

Initially the cooldown was on the enemy and it couldn't recieve damage while on cooldown, but now i want to apply diferent types of damages simultaneously so this doesn't work.

Am i doing this wrong ?

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 KhenaB · May 31, 2017 at 07:49 AM

Well i've solved my problem by using a List, seems to work properly, example:

 private List<Collider2D> hitEnemies = new List<Collider2D>();
 
 void OnTriggerStay2D(Collider2D enemy)
      {
          if (other.tag == "Enemy" && !hitEnemies.Contains(enemy))
          {
              StartCoroutine(Damage(enemy));
          }
      }
  
  
      IEnumerator Damage(Collider2D enemy)
      {
          hitEnemies.Add(enemy);
  
          enemyHealth = enemy.GetComponent<EnemyHealth>();
          enemyHealth.Damage(damageAmount);
  
          yield return new WaitForSeconds(damageCooldown);
  
          hitEnemies.Remove(enemy);
      }
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 lefinno · May 31, 2017 at 08:12 AM 0
Share

Thanks alot!

avatar image
0

Answer by ShadyProductions · May 31, 2017 at 07:03 AM

foreach enemy that is in the trigger you will have to use a Coroutine and maybe check something on the enemy like a bool to know if he already has this debuf so you won't apply it multiple times to the same enemy. StartCoroutine(YourDamageMethod(enemy));

 IEnumerator YourDamageMethod(Gameobject go) {
 //do some damage to the go
 }
Comment
Add comment · Show 3 · 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 KhenaB · May 31, 2017 at 07:10 AM 0
Share

So you're suggesting that i move the cooldown to the enemy like i did before, but have a cooldown for each attack types?

I wanted to have the cooldown on the weapon so that it could be different for each weapons that uses OnTriggerStay2D to damage the enemy.

avatar image ShadyProductions KhenaB · May 31, 2017 at 07:17 AM 0
Share

Just use a coroutine to trigger the damage & the cooldown.

avatar image KhenaB ShadyProductions · May 31, 2017 at 07:25 AM 0
Share

This is an example of a basic cooldown that i did for a weapon that does damage while it stays over an enemy, but the issue is that it will only hit one enemy per tick. I don't think i understand your suggestion.

  void OnTriggerStay2D(Collider2D enemy)
     {
         if (other.tag == "Enemy" && !doingDamage)
         {
             StartCoroutine(Damage(enemy));
         }
     }
 
 
     IEnumerator Damage(Collider2D enemy)
     {
         doingDamage = true;
 
         enemyHealth = enemy.GetComponent<EnemyHealth>();
         enemyHealth.Damage(damageAmount);
 
         yield return new WaitForSeconds(damageCooldown);
 
         doingDamage = false;
     }

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

66 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

Related Questions

Damage on prefab collision? 0 Answers

Enemy Damage to Player 0 Answers

Shooting and damage with raycast doesnt work. 0 Answers

Player attacks once but the enemy takes double damages! 1 Answer

auto select enemys 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges