画面上の敵を一掃する方法

画面上の敵を一掃する
using UnityEngine;
using System.Collections;
public class Bomb : MonoBehaviour {
private GameObject[] targets;
public GameObject effectPrefab;
public GameObject[] itemPrefabs;
void Update(){
// 敵のデータを配列形式で取得する。(EnemyのTagが付いている敵のデータを取得する)
targets = GameObject.FindGameObjectsWithTag("Enemy");
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("Shell")){
Destroy(gameObject);
Destroy(other.gameObject);
for(int i = 0; i < targets.Length; i++){
// 敵を全滅させる
Destroy(targets[i]);
// 敵の位置でエフェクトを発生させる
Instantiate(effectPrefab, targets[i].transform.position, Quaternion.identity);
// ドロップさせるアイテムをランダムに決める。
GameObject item = itemPrefabs[Random.Range(0, itemPrefabs.Length)];
// 敵の位置でアイテムを発生させる
Instantiate(item, targets[i].transform.position, Quaternion.identity);
}
}
}
}
Unity Code Memo
他のコースを見る
画面上の敵を一掃する
using UnityEngine;
using System.Collections;
public class Bomb : MonoBehaviour {
private GameObject[] targets;
public GameObject effectPrefab;
public GameObject[] itemPrefabs;
void Update(){
// 敵のデータを配列形式で取得する。(EnemyのTagが付いている敵のデータを取得する)
targets = GameObject.FindGameObjectsWithTag("Enemy");
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("Shell")){
Destroy(gameObject);
Destroy(other.gameObject);
for(int i = 0; i < targets.Length; i++){
// 敵を全滅させる
Destroy(targets[i]);
// 敵の位置でエフェクトを発生させる
Instantiate(effectPrefab, targets[i].transform.position, Quaternion.identity);
// ドロップさせるアイテムをランダムに決める。
GameObject item = itemPrefabs[Random.Range(0, itemPrefabs.Length)];
// 敵の位置でアイテムを発生させる
Instantiate(item, targets[i].transform.position, Quaternion.identity);
}
}
}
}
画面上の敵を一掃する方法