C# Mathf.PerlinNoise仅在每个给定位置返回浮点0.4652731

C# Mathf.PerlinNoise仅在每个给定位置返回浮点0.4652731,c#,unity3d,C#,Unity3d,我正在尝试编写一个基于2d tilemap的world generationscript,但是Mathf.PerlinNoise命令只返回0.4652731 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Tilemaps; public class WorldGeneration : MonoBehaviour { public Tile

我正在尝试编写一个基于2d tilemap的world generationscript,但是Mathf.PerlinNoise命令只返回0.4652731

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

public class WorldGeneration : MonoBehaviour
{

    public Tilemap Tilemap;
    public Tile GrassTile;

    public int width;
    public int height;

    public float threshold;

    void Start()
    {
        Tilemap.ClearAllTiles();

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Debug.Log(Mathf.PerlinNoise(x, y));
                
                if (Mathf.PerlinNoise(x, y) >= threshold)
                {
                    Tilemap.SetTile(new Vector3Int(x, y, 0), GrassTile);
                }
            }
        }

    }

}

执行脚本后,没有任何错误!为什么Mathf.PerlinNoise命令只返回相同的数字?

这是因为您正在向函数传递整数值。你应该缩小你的x和y值。Brackeys有一个很好的教程,我建议你看。 Afaik生成大小为1*1的图案。不确定它是否不会重复

我猜想,在您的情况下,您传递的是整数,因此它总是返回相同/相似的值

尝试将其缩小到从0到1的分数,例如

Debug.Log(Mathf.PerlinNoise(x / width, y / height));
参见,例如,哪个问题完全相同

他们都印了相同的号码:0.4652731

function Start () 
{
     print(Mathf.PerlinNoise(0, 0));
     print(Mathf.PerlinNoise(2, 0));
     print(Mathf.PerlinNoise(0, 5));
     print(Mathf.PerlinNoise(10, 3));
     print(Mathf.PerlinNoise(2, 0));
     print(Mathf.PerlinNoise(1, 1));
}