- Home /
Question by
maikol_hrvatin · Nov 15, 2018 at 04:11 PM ·
triggerboxcollider2darea-damage
How to make OnTriggerEnter2D affect only one collider?
I'm making a tower defence game in which every attack creates a new object (DamageArea) with a boxcollider2d which is used to damege units. After a collision or a short period of time the object (DamageArea) is destroyed. The problem is that currently all enemies that collide with the object get damaged. How can i change the OnTriggerEnter2D to damage only one enemy at the time?
public class HurtUnit : MonoBehaviour {
public string side;
public int damage;
public bool isAreaAttack;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
//if (other.gameObject.name != "AgroArea" || other.gameObject.name != "DamageArea") //check if unit
if(other.gameObject.GetComponent<HealthManager>() != null) //check if it has health
{
if(other.gameObject.GetComponent<HealthManager>().side != side) //check if enemy
{
other.gameObject.GetComponent<HealthManager>().DamageUnit(damage);
Destroy(gameObject);
}
}
}
}
Comment