敵を一体倒すともう一体出てくるだけでは、あまり面白くないため
常時数体表示するように変更する
Hierarchyに空のオブジェクトを作り
名前を、EnemyGeneratorとする。
そこに、EnemyGeneratorCtrl.csを付ける。

EnemyGeneratorCtrl.csの中身を
using UnityEngine;
using System.Collections;
public class EnemyGeneratorCtrl : MonoBehaviour {
// 生まれてくる敵プレハブ
public GameObject enemyPrefab;
// 敵を格納
GameObject[] existEnemys;
// アクティブの最大数
public int maxEnemy = 2;
void Start()
{
// 配列確保
existEnemys = new GameObject[maxEnemy];
// 周期的に実行したい場合はコルーチンを使うと簡単に実装できます。
StartCoroutine(Exec());
}
// 敵を作成します
IEnumerator Exec()
{
while(true){
Generate();
yield return new WaitForSeconds( 3.0f );
}
}
void Generate()
{
for(int enemyCount = 0; enemyCount < existEnemys.Length; ++ enemyCount)
{
if( existEnemys[enemyCount] == null ){
existEnemys[enemyCount] = Instantiate(enemyPrefab,transform.position,transform.rotation) as GameObject;
return;
}
}
}
}
ーーーーーーーーーーーーーー
- 関連記事
-
YouTubeで動画を定期的に出しています。ご視聴いただけると嬉しいです。
Reon Labo
- 2016/12/22(木) 18:00:00|
- Unity
-
| トラックバック:0
-
| コメント:0