gpt4 book ai didi

c# - 为什么 Unity 卡在 Application.EnterPlayMode 上?

转载 作者:行者123 更新时间:2023-12-05 01:35:13 25 4
gpt4 key购买 nike

我正在尝试使用 perlin 噪声和统一行进立方体创建程序地形生成器。它一直有效,直到我从创建高度图切换到将其制作成 3d 数组。然后,每当我点击播放时,unity 都会打开一个对话框,其中写着 Application.EnterPlayMode,它不会消失,也不会进入播放模式。当它发生时一切都停止响应,他们停止它的唯一方法是在任务管理器中将其杀死。

有问题的脚本如下:

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

public class Noise : MonoBehaviour
{
//Determines whether to show debug values
public bool debug = false;

//Determines flatness of the terrain
public float noiseScale = 0.5f;

//Type of perlin noise to use
public enum PerlinNoise {
twoD,
threeD
};
public PerlinNoise perlinNoiseDimension = PerlinNoise.twoD;

//To return noise data after all calculations
public float[,,] getTerrainData(int x, int y, int z)
{
float[,,] terrainData = new float[x, y, z];

if(perlinNoiseDimension == PerlinNoise.twoD)
{
terrainData = PerlinNoise2D(x, y, z, noiseScale);
}
return terrainData;
}

//Determine noise values using 2D Perlin noise
private float[,,] PerlinNoise2D(int x, int y, int z, float noiseScale)
{
float[,,] voxelHeights = new float[x, y, z];

if (debug)
{
Debug.Log("Heightmap");
}

//Origin points to sample from
float xOrg = Random.Range(0.0f, 0.9999999f);
float yOrg = Random.Range(0.0f, 0.9999999f);

for (int currentx = 0; currentx < x; currentx++)
{
for (int currenty = 0; currenty < y; currenty++)
{
//Convert Values to Fractions
float xCoord = (float)currentx / (x * noiseScale) + xOrg;
float yCoord = (float)currenty / (y * noiseScale) + yOrg;

float height = Mathf.PerlinNoise(xCoord, yCoord) * z;

for(int currentz = 0; currentz <= height; z++)
{
voxelHeights[currentx, currenty, currentz] = 1;
}

if (debug)
{
Debug.Log("Height = " + height + ", X = " + currentx + ", Y = " + currenty + ", X Coord = " + xCoord + ", Y Coord = " + yCoord);
}
}

}
return voxelHeights;
}
}

它显示的图像如下:

The error

最佳答案

你正在造成一个永无止境的循环

for(int currentz = 0; currentz <= height; z++)
{
voxelHeights[currentx, currenty, currentz] = 1;
}

这里你增加了z++但是你的循环条件是 currentz <= height .在循环中,您永远不会更新 currentz 的值也不是 height 的值所以你的循环永远不会结束。

由于索引的使用,它可能更应该是

for(int currentz = 0; currentz <= height; currentz++)
{
voxelHeights[currentx, currenty, currentz] = 1;
}

但是不确定如何height在这里发挥作用是因为我希望它看起来有点像例如

for(int currentz = 0; currentz < z; currentz++)
{
voxelHeights[currentx, currenty, currentz] = height;
}

这对我来说似乎更有意义。

关于c# - 为什么 Unity 卡在 Application.EnterPlayMode 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63166053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com