1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Unityで横スクロールゲームの敵タイプ別自動生成

Last updated at Posted at 2023-11-14
Unityで横スクロールゲームの敵タイプ別自動生成
EnemySpawn.cs
public class EnemySpawn : MonoBehaviour
{
    [SerializeField] List<EnemyCount> enemyCounts;

    private List<GameObject> spawnedEnemies = new List<GameObject>();   // 生成済みの敵のリスト

    private void Start()
    {
        // 初期配置
        SpawnEnemies();
    }

    private void SpawnEnemies()
    {
        for (int i = 0; i <= 1; i++)
        {
            EnemyGenerator(enemyCounts[i]);
        }
    }

    private void EnemyGenerator(EnemyCount data)
    {
        if (data.isSpawn)
        {
            foreach (Vector3 p in data.pos)
            {
                // 敵を生成してリストに追加する
                GameObject enemyObj = Instantiate(data.enemyPrefabs, p, Quaternion.identity);
                spawnedEnemies.Add(enemyObj);
                enemyObj.SetActive(true);
            }
        }
    }


    public void SpawnEnemy()
    {
        SpawnEnemies();
    }

    public List<GameObject> GetSpawnedEnemies()
    {
        return spawnedEnemies;
    }
}

[System.Serializable]
public class EnemyCount
{
    public enum ENEMY
    {
        Enemy1,
        Enemy2,
        Enemy3,
        Enemy4,
        Enemy5,
        Enemy6,
        // これがラベルになる
    }

    public ENEMY enemy;
    public List<Vector3> pos;
    public GameObject enemyPrefabs;
    public bool isSpawn;
}

こちらのスクリプトでは敵キャラクタを自動生成するのに役立ちます。
EnemySpawnクラス外のEnemyCountクラスでは敵タイプ、座標、プレハブ、生成数を管理できます。

修正版ではbool型変数を追加してインスペクターからチェックをオンオフすることで生成するかしないかを管理できます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?