Unity - PerlinNoise提供相同的号码

时间:2016-07-28 19:48:12

标签: c# 2d unity5 generated

我正在建立一个基于磁贴的游戏,我想为玩家创造一个探索的世界。出于某种原因,Mathf.PerlinNoise()有时会一遍又一遍地给出相同的数字,即使每次给出的XY不同。

void Start () {
    treeOffsetX = Mathf.Round(Random.Range (10f,300000f));
    treeOffsetY = Mathf.Round(Random.Range (10f,300000f));
    waterOffsetX = Mathf.Round(Random.Range (10f,300000f));
    waterOffsetY = Mathf.Round(Random.Range (10f,300000f));
    boulderOffsetX = Mathf.Round(Random.Range (10f,300000f));
    boulderOffsetY = Mathf.Round(Random.Range (10f,300000f));

    for (float x = -gridX; x < gridX; x += 0.1f) 
    {
        for (float y = -gridY; y < gridY; y += 0.1f) {
            Vector3 spawnPos = new Vector3 (x * 10, y * 10, 0);
            float perlinTree = Mathf.PerlinNoise ((x + treeOffsetX) / seedTreeX, (y + treeOffsetY) / seedTreeY);
            float perlinWater = Mathf.PerlinNoise ((x + waterOffsetX) / seedWaterX, (y + waterOffsetY) / seedWaterY);
            float perlinBoulder = Mathf.PerlinNoise ((x + boulderOffsetX) / seedBoulderX, (y + boulderOffsetY) / seedBoulderY);


            if (perlinWater > perlinThresholdWater) {
                GameObject waterTile = Instantiate (water, spawnPos, Quaternion.identity) as GameObject;
            } else if (perlinBoulder > perlinThresholdBoulder) {
                GameObject grassTile = Instantiate (grass, spawnPos, Quaternion.identity) as GameObject;
                GameObject boulderTile = Instantiate (boulder, spawnPos, Quaternion.identity) as GameObject;
            } else if (perlinTree > perlinThresholdTree) {
                GameObject grassTile = Instantiate (grass, spawnPos, Quaternion.identity) as GameObject;
                GameObject treeTile = Instantiate (tree, spawnPos, Quaternion.identity) as GameObject;
            } else {
                GameObject grassTile = Instantiate (grass, spawnPos, Quaternion.identity) as GameObject;
            }
        }
    }
}

因此,有时perlinTree或perlinBoulder每次通过循环时都会给出相同的数字。然后将输出没有树木或巨石。我对水没问题。

它有时可以工作,有时候不工作吗?

0 个答案:

没有答案