- Home /
Hitting Multiple Enemies
So i got this script to target tags with enemy in them. But the problem is it only hits one enemy till it dies then goes to the next enemy with the tag enemy in it. How do i make it so that i can hit the enemies all at once if they are in reach of the collision. using UnityEngine; using System.Collections;
public class PlayerAttack : MonoBehaviour {
public float attackTimer;
public float coolDown;
private bool attack;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 1;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetMouseButtonUp(0)){
if(attackTimer == 0){
if (attack = true){
GameObject.FindGameObjectWithTag("Enemy").GetComponent<EnemyHealth>().AdjustCurrentHealth(-10);
}
attackTimer = coolDown;
}
}
}
void OnCollisionEnter2D(Collision2D coll)
{
if(coll.gameObject.tag == "Enemy")
{
attack = true;
}
}
}
Answer by Ricewind1 · Jun 11, 2015 at 04:57 AM
Best bet is to cast an overlapsphere at the point of collision with an enemy, the hit every gameobject with the tag "enemy" within the radius of that overlapshpere.
http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
Your answer
Follow this Question
Related Questions
Multiple Melee Weapons 2 Answers
noob question about melee combat 1 Answer
A Fighting Games Hit boxes 1 Answer
Brawl style melee combat? 0 Answers
2D One, Two, Three Attack Combo 2 Answers