产生随机平台的统一

时间:2018-06-03 13:44:39

标签: unity3d

因此,在我想要制作像非常流行的曲折游戏这样的游戏时,我陷入了随机生成平台的困境。平台在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,依此类推。但是有两个主要问题。

enter image description here

PS:那些小黑方块只不过是钻石(忽略它们)。

它们要么形成2X2块,要么相互重叠。

我该如何避免这些?或者是否有一种更简单的方法来生成我缺少的平台。

1 个答案:

答案 0 :(得分:2)

您需要一个数组来指示哪些图块被占用:

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
                {
                     ...
                }

请注意,此代码不起作用,它只是您进一步开发它的起点。