【问题标题】:Random tile-based map generation like Terraria in unity?像泰拉瑞亚这样的基于随机图块的地图生成是统一的吗?
【发布时间】:2014-10-29 20:15:31
【问题描述】:

我想使用 Unity 和 C# 从游戏 Terraria 和 Starbound 中的图块生成地图 到目前为止我所做的一切都不起作用,我不知道为什么......有任何帮助/想法/建议吗?

using UnityEngine;
using System.Collections;

public class MapGeneration : MonoBehaviour {
public int mapWidth, mapHeight, spreadDegree;
GameObject[,] blocks;
public GameObject baseBlock;

public int maxHeightAllowed;
public int nullDispersion;
int dispersionZone;

void Start() {
    GenerateMap();
}

void GenerateMap() {
    dispersionZone = maxHeightAllowed - nullDispersion;
    blocks = new GameObject[mapWidth, mapHeight];
    for(int x = 0; x < blocks.GetLength(0); x++) {
            for(int y = nullDispersion; y <= maxHeightAllowed; y++) {
                int heightDiff = y - nullDispersion;
                float dispersionModifier = Mathf.Sin( (heightDiff / dispersionZone) * 90);
                float random = Random.Range(0, 1);

                if(random > dispersionModifier) {
                    blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity);
                } else if (y < blocks.GetLength(1) && blocks[x,y+1] != null) {
                    blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity);
                }
            }

            for(int y = 0; y < nullDispersion; y++) {
                blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity);
            }
    }
}
}

【问题讨论】:

  • 你需要说 what 不起作用。到目前为止,我只看到了一个代码转储。
  • 啊..目前它只生成一个实心块...它应该创建一个像泰拉瑞亚一样的随机地图...:)

标签: c# random map unity3d procedural


【解决方案1】:
(heightDiff / dispersionZone)

这是整数除法。请改用(heightDiff / (float)dispersionZone)

* 90

Mathf.Sin 以弧度工作,而不是度数。使用* Math.PI/2

            if(random > dispersionModifier) {
                blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity);
            } else if (y < blocks.GetLength(1) && blocks[x,y+1] != null) {
                blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity);
            }

为什么在创建相同类型的块时使用 if? if 和 else 中的 body 不应该有某种区别吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2016-05-15
    • 2012-03-22
    相关资源
    最近更新 更多