所以,在我想要制作一个像极受欢迎的zigzag游戏一样的游戏的时候,我被困在随机生成的平台上。平台按X、-X、Z、-Z方向随机生成。我已经编写了我的代码,它在那里生成平台。我可能会采取一种很长的方法(如果有的话)
void Start ()
{
lastPos = platform.transform.position;
size = platform.transform.localScale.x;
InvokeRepeating("SpawnXZ",1f,0.2f);
}
void SpawnX()
{
Vector3 pos = lastPos;
pos.x += size;
lastPos = pos;
Instantiate(platform, pos, Quaternion.identity);
}
void SpawnZ()
{
Vector3 pos = lastPos;
pos.z += size;
lastPos = pos;
Instantiate(platform, pos, Quaternion.identity);
}
void SpawnNegX()
{
Vector3 pos = lastPos;
pos.x -= size;
lastPos = pos;
Instantiate(platform, pos, Quaternion.identity);
}
void SpawnNegZ()
{
Vector3 pos = lastPos;
pos.z -= size;
lastPos = pos;
Instantiate(platform, pos, Quaternion.identity);
}
void SpawnXZ()
{
int rand = Random.Range(0, 6);
if (rand < 3)
{
SpawnX();
}
else if(rand >= 3)
{
SpawnZ();
}
if(--counter == 0) { CancelInvoke("SpawnXZ"); };
if(counter == 0)
{
counter = 25;
int r = Random.Range(0,2);
if(r == 0)
{
InvokeRepeating("SpawnNegXZ",0f,0.2f);
}
else
{
InvokeRepeating("SpawnXNegZ",0f,0.2f);
}
}
}
void SpawnNegXZ()
{
int rand = Random.Range(0, 6);
if (rand < 3)
{
SpawnNegX();
}
else if(rand >= 3)
{
SpawnZ();
}
if(--counter == 0) { CancelInvoke("SpawnNegXZ"); };
if(counter == 0)
{
counter = 25;
int r = Random.Range(0,2);
if(r == 0)
{
InvokeRepeating("SpawnXZ",0f,0.2f);
}
else
{
InvokeRepeating("SpawnNegXNegZ",0f,0.2f);
}
}
}
void SpawnXNegZ()
{
int rand = Random.Range(0, 6);
if (rand < 3)
{
SpawnX();
}
else if(rand >= 3)
{
SpawnNegZ();
}
if(--counter == 0) { CancelInvoke("SpawnXNegZ"); };
if(counter == 0)
{
counter = 25;
int r = Random.Range(0,2);
if(r == 0)
{
InvokeRepeating("SpawnXZ",0f,0.2f);
}
else
{
InvokeRepeating("SpawnNegXNegZ",0f,0.2f);
}
}
}
void SpawnNegXNegZ()
{
int rand = Random.Range(0, 6);
if (rand < 3)
{
SpawnNegX();
}
else if(rand >= 3)
{
SpawnNegZ();
}
if(--counter == 0) { CancelInvoke("SpawnNegXNegZ"); };
if(counter == 0)
{
counter = 25;
int r = Random.Range(0,2);
if(r == 0)
{
InvokeRepeating("SpawnNegXZ",0f,0.2f);
}
else
{
InvokeRepeating("SpawnXNegZ",0f,0.2f);
}
}
}我打过xz,-xz,x -z和-x -z。我首先调用X和Z方向的平台产卵,然后切换到-XZ或x -Z等等。但主要有两个问题。

PS :那些小黑方块只不过是钻石(忽略它们)。
它们要么形成一个2X2块,要么相互重叠。
我该怎么避免这些?或者有一种更简单的方法来生成我所缺少的平台。
发布于 2018-06-03 15:39:35
您需要一个数组来指示占用了哪些瓷砖:
bool[,] tiles = new bool[N,N];或者一本字典
Dictionary<XZ, bool> tiles = new Dictionary<XZ, bool>();
public struct XZ { public int X; public int Z; }每当生成瓷砖时,请检查该值,以确定是否有可能生成:
void SpawnXZ()
{
int x = (int) (lastpos.x / size);
int z = (int) (lastpos.z / size);
int rand = Random.Range(0, 6);
if (rand < 3 && !tiles[x + 1, z])
{
SpawnX();
tiles[x + 1, z] = true;
}
else if(rand >= 3 && && !tiles[x, z + 1])
{
SpawnZ();
tiles[x, z + 1] = true;
}
else
{
...
}请注意,这段代码将不能工作,它只是进一步开发它的起点。
https://stackoverflow.com/questions/50667051
复制相似问题