private void OnTriggerEnter2D(Collider2D collision) { //Creamos un a variable proyectile, para acceder al script proyectile y le damos el mismo behaviur que en PlayerController Proyectile missile = collision.gameObject.GetComponent <Proyectile>(); if (missile) { health -= missile.GetDamage(); missile.Hit(); if (health <= 0) { EnemyDown(); } Debug.Log("Enemy hit!"); } }
/// <summary> /// Cuando colisiona un misil lanzado con un objeto, tanto enemigo como aliado /// </summary> /// <param name="collision"></param> private void OnTriggerEnter2D(Collider2D collision) { //Llamamos al script Proyectile en el cual hemos creado el proyectil, dandole un daño editable al chocar con un objeto //y se lo restará a la vida. Proyectile missile = collision.gameObject.GetComponent <Proyectile>(); //Si el misil choca con un el jugador o con un enemigo, restará el valor del daño a la vida if (missile) { //Restamos a la variable health, el valor retornado de la función GetDamage() creada en el script Proyectile //Esta función retorna el valor del daño que causa un misil. //Restamos ese valor a heath health -= missile.GetDamage(); missile.Hit(); //Llamamos a la función Die() si la vida es igaul o menor a 0 if (health <= 0) { Die(); } Debug.Log("Enemy hit!"); } }