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