敵の発生装置の作成②(ランダムの活用)
![B5ba48f2 9ecb 4fbc 978c 47081c44882e](https://codegenius.org/uploads/slide/image/7622/b5ba48f2-9ecb-4fbc-978c-47081c44882e.jpeg)
発生する敵が毎回ランダム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyGene : MonoBehaviour
{
// ★改良
public GameObject[] enemyPrefabs;
void Start()
{
StartCoroutine(Gene());
}
private IEnumerator Gene()
{
while (true)
{
for (int j = 0; j < 5; j++)
{
// ★追加
// 毎回ランダムにnumの数字が決まる。
int num = Random.Range(0, enemyPrefabs.Length);
// ★改良
Instantiate(enemyPrefabs[num], transform.position, Quaternion.identity);
yield return new WaitForSeconds(0.2f);
}
yield return new WaitForSeconds(1f);
}
}
}
![0663e6cd d031 4f83 b6b1 af65e24f088a](https://codegenius.org/uploads/slide/image/7623/0663e6cd-d031-4f83-b6b1-af65e24f088a.jpeg)
![21698dd3 f47e 4249 883a 050de7a84085](https://codegenius.org/uploads/slide/image/7624/21698dd3-f47e-4249-883a-050de7a84085.jpeg)
![2fb56a40 e436 46db 93ee bc5b7dc93272](https://codegenius.org/uploads/slide/image/7625/2fb56a40-e436-46db-93ee-bc5b7dc93272.jpeg)
【2021版】Danmaku(基礎/全55回)
他のコースを見る![B5ba48f2 9ecb 4fbc 978c 47081c44882e](https://codegenius.org/uploads/slide/image/7622/b5ba48f2-9ecb-4fbc-978c-47081c44882e.jpeg)
発生する敵が毎回ランダム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyGene : MonoBehaviour
{
// ★改良
public GameObject[] enemyPrefabs;
void Start()
{
StartCoroutine(Gene());
}
private IEnumerator Gene()
{
while (true)
{
for (int j = 0; j < 5; j++)
{
// ★追加
// 毎回ランダムにnumの数字が決まる。
int num = Random.Range(0, enemyPrefabs.Length);
// ★改良
Instantiate(enemyPrefabs[num], transform.position, Quaternion.identity);
yield return new WaitForSeconds(0.2f);
}
yield return new WaitForSeconds(1f);
}
}
}
![0663e6cd d031 4f83 b6b1 af65e24f088a](https://codegenius.org/uploads/slide/image/7623/0663e6cd-d031-4f83-b6b1-af65e24f088a.jpeg)
![21698dd3 f47e 4249 883a 050de7a84085](https://codegenius.org/uploads/slide/image/7624/21698dd3-f47e-4249-883a-050de7a84085.jpeg)
![2fb56a40 e436 46db 93ee bc5b7dc93272](https://codegenius.org/uploads/slide/image/7625/2fb56a40-e436-46db-93ee-bc5b7dc93272.jpeg)
敵の発生装置の作成②(ランダムの活用)