(Unity)WaveSpawn(ウェーブ・スポーン)の作り方

(スクリプト)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaveSpawn : MonoBehaviour
{
    public GameObject[] bots;

    private int waveNum = 1;
    private int botNum = 0;

    void Start()
    {
        StartCoroutine(BotGene());
    }

    private IEnumerator BotGene()
    {
        while(true)
        {
            for (int j = 0; j < waveNum; j++)
            {
                Instantiate(bots[botNum], transform.position, Quaternion.identity);
                yield return new WaitForSeconds(1f);
            }

            waveNum += 1;

            yield return new WaitForSeconds(1f);
        }
    }
}

(実行結果)


(改良1;ウェーブごとに出現する敵をランダム化する)

(スクリプト)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaveSpawn : MonoBehaviour
{
    public GameObject[] bots;

    private int waveNum = 1;
    private int botNum = 0;

    void Start()
    {
        StartCoroutine(BotGene());
    }

    private IEnumerator BotGene()
    {
        while(true)
        {
            for (int j = 0; j < waveNum; j++)
            {
                Instantiate(bots[botNum], transform.position, Quaternion.identity);
                yield return new WaitForSeconds(1f);
            }

            waveNum += 1;

            // 改良(ランダム化のテクニック)
            botNum = Random.Range(0, bots.Length);

            yield return new WaitForSeconds(1f);
        }
    }
}

(実行結果)


(改良2;複数の敵を順番にスポーンする。ループ機能あり)

(スクリプト)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaveSpawn : MonoBehaviour
{
    public GameObject[] bots;

    private int waveNum = 1;
    private int botNum = 0;

    void Start()
    {
        StartCoroutine(BotGene());
    }

    private IEnumerator BotGene()
    {
        while(true)
        {
            for (int j = 0; j < waveNum; j++)
            {
                Instantiate(bots[botNum], transform.position, Quaternion.identity);
                yield return new WaitForSeconds(1f);
            }

            waveNum += 1;

            // 改良(ランダム化のテクニック)
            //botNum = Random.Range(0, bots.Length); いったんコメントアウトする。

            // 改良2(順送りのテクニック)
            botNum = (botNum + 1) % bots.Length;

            yield return new WaitForSeconds(1f);
        }
    }
}

(実行結果)