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 /
  • Help Room /
avatar image
0
Question by matthewbev12 · Dec 15, 2018 at 08:38 PM · enemyfunctionintegerplayer damage

Player deals damage to all enemies

Hello, I am a little new to unity (started about 3 months ago), and I am currently having an issue with my attack script. I used the following video to make my character's attack system: https://www.youtube.com/watch?v=1QfxdUpVh5I&t=388s&list=WL∈dex=3 However, when attacking one enemy all of my enemies with the "TakeDamage" function will take damage. How would I fix this and only deal damage to only the enemies within the attack Range?

Scripts: attack script

 private float timeBtwAttack;
 public float startTimeBtwAttack;
 public Transform attackPos;
 public float attackRange;
 public LayerMask WhatIsEnemies;
 public int damage = 1;
 void Update()
 {
     if (timeBtwAttack <= 0) {
         if (Input.GetKey (KeyCode.R)) {
             timeBtwAttack = startTimeBtwAttack;
             Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll (attackPos.position, attackRange, WhatIsEnemies);
             for (int i = 0; i < enemiesToDamage.Length; i++) {
                 enemiesToDamage [i].GetComponent<EnemyHealthect> ().TakeDamage (damage);
             }
         }
     

     } else {
         timeBtwAttack -= Time.deltaTime;
     }
 }

 void OnDrawGizmosSelected(){
     Gizmos.color = Color.red;
     Gizmos.DrawWireSphere (attackPos.position, attackRange);
 }

}

and The enemy script:

 void Awake (){
     Instance = this;
 

 }

 private void Flash(){

     EnemyHealthect.Instance.StartCoroutine(ColorChange());
 }
 private IEnumerator ColorChange(){
     var Renderer = SpriteRenderer;
     if (GetComponent<Renderer>() != null) {
         for (int i = 1; i <= timesToFlash; i++) {
             GetComponent<Renderer>().material.color = FlashColor;
             yield return new WaitForSeconds (FlashDelay);
             GetComponent<Renderer>().material.color = NormalColor;
             yield return new WaitForSeconds (FlashDelay);
         }
         }
     }


     


     void Update() {
         if (health<=0){
         health = 0;
         Destroy (gameObject);

     }

 }
 public void TakeDamage(int damage){
     
     health -= damage;
     EnemyHealthect.Instance.Flash ();
     Debug.Log (health);
 }


}

thanks

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 Hellium · Dec 15, 2018 at 09:09 PM 0
Share

Is health declared static?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Vega4Life · Dec 15, 2018 at 09:31 PM

Well, you are using overlapcircleall, which will return every collider it overlaps. Then you are iterating over this list and sending damage to all of them.


              Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll (attackPos.position, attackRange, WhatIsEnemies);
              for (int i = 0; i < enemiesToDamage.Length; i++) {
                  enemiesToDamage [i].GetComponent<EnemyHealthect> ().TakeDamage (damage);
              }



Maybe just do overlapcircle, so it comes back with just one collider.


 Collider2D enemyToDamage = Physics2D.OverlapCircle(attackPos.position, attackRange, WhatIsEnemies);
 enemyToDamage.GetComponent<EnemyHealthect>().TakeDamage(damage);



If your intent was to damage all the enemies within the radius, then you should check how big your radius is. It could be monstrous. You can a draw a gizmo to show it in the scene view, or attach a sphere to your character and match the size as the radius you intend to hit things. This is a good way to show the visual of the range.


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 matthewbev12 · Dec 16, 2018 at 08:48 AM 0
Share

Thanks for your reply Vega4life! I have tried drawing a gizmo in the scene view, and the radius was of a reasonable size. I also noticed that when I attacked an enemy, an error in the console would show up. It said "Object reference not set to the instance of a AttackScript.Update()." Also I'm intending to use overlapcircleall btw.

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

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

Can OnTriggerEnter2D trigger again once triggered? 0 Answers

Basic Enemy follow script for Unity five, desperately needed! 0 Answers

Enemies cannot kill Player. 0 Answers

Enemy Isn't Hurting Player in Survival Shooter 0 Answers

Move enemy towards player if enemy is in Camera bounds? 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