0
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?

Unityマイクラ風の地形の生成です。ChatGPTで作りました。

Last updated at Posted at 2024-12-06

csファイルを保存して、cubeにアタッチします。プレハブはcubeでいいです。

using UnityEngine;
using System.Collections;

public class VoxelTerrain : MonoBehaviour
{
    public GameObject grassBlock;  // 草地用ブロック
    public GameObject sandBlock;   // 砂地用ブロック
    public GameObject snowBlock;   // 雪地用ブロック
    public GameObject treeBlock;   // 木用のブロック
    public int width = 100;        // 地形の幅
    public int depth = 100;        // 地形の奥行き
    public int maxHeight = 10;     // 地形の最大高さ
    public float treeDensity = 0.1f; // 木の密度(0.0f~1.0f)

    private bool isGenerating = false; // 生成中フラグ

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

    IEnumerator GenerateTerrainCoroutine()
    {
        isGenerating = true;

        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < depth; z++)
            {
                // 高さを計算
                int height = Mathf.FloorToInt(Mathf.PerlinNoise(x * 0.3f, z * 0.3f) * maxHeight);
                GameObject blockType = SelectBiomeBlock(Mathf.PerlinNoise(x * 0.1f, z * 0.1f));

                for (int y = 0; y <= height; y++)
                {
                    Vector3 position = new Vector3(x, y, z);

                    if (y == height)
                    {
                        // 最上層ブロック
                        InstantiateBlock(blockType, position);

                        // 木の生成
                        if (Random.value < treeDensity && y < maxHeight - 1)
                        {
                            // 木の生成位置を調整
                            GenerateTree(x, y + 1, z);
                        }
                    }
                    else
                    {
                        // 下層は砂
                        InstantiateBlock(sandBlock, position);
                    }
                }

                // フレームごとに少しずつ処理を実行
                if (z % 10 == 0) yield return null;
            }

            if (x % 5 == 0) yield return null;
        }

        isGenerating = false;
    }

    GameObject SelectBiomeBlock(float noise)
    {
        if (noise < 0.33f) return grassBlock; // 草地
        if (noise < 0.66f) return sandBlock;  // 砂地
        return snowBlock;                     // 雪原
    }

    void InstantiateBlock(GameObject blockType, Vector3 position)
    {
        // ブロックを生成し、回転を適用
        Instantiate(blockType, position, Quaternion.Euler(-90f, 0f, 0f));
    }

    void GenerateTree(int x, int y, int z)
    {
        // 木の高さをランダムに設定
        int treeHeight = Random.Range(3, 7);

        // 木の生成位置を決定(地面より上に木を生成)
        for (int i = 0; i < treeHeight; i++)
        {
            Vector3 treePosition = new Vector3(x, y + i, z);

            // 木の生成位置がmaxHeightより高くならないように制限
            if (treePosition.y <= maxHeight)
            {
                // 木のブロックを生成
                Instantiate(treeBlock, treePosition, Quaternion.identity);
            }
        }
    }

    public bool IsGenerating()
    {
        return isGenerating;
    }
}

0
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
0
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?