【问题标题】:Create 3 dimensional grid two elements, one element in one row but in random columns创建 3 维网格两个元素,一个元素在一行中,但在随机列中
【发布时间】:2014-07-28 03:31:04
【问题描述】:

我创建了一个 3 维网格。我有两个单独的对象填充这个网格的空间。我想让其中一个对象在一行中,但在随机选择的列上。

以前有没有人这样做过,或者谁能指出我正确的方向?

我正在使用 Unity 和 C#。谢谢。

    Vector3 towerSize = new Vector3(3, 3, 3);

//create grid tower
for (int x = 0; x < towerSize.x; x++)
{
    for (int z = 0; z < towerSize.z; z++)
    {
        for (int y = 0; y < towerSize.y; y++)
        {
            //spawn tiles and space them
            GameObject obj = (GameObject)Instantiate(tiles);
            obj.transform.position = new Vector3(x * 1.2f, y * 1.2f, z * 1.2f);

            //add them all to a List
            allTiles.Add(obj);
            obj.name = "tile " + allTiles.Count;
        }
    }
}

有网格的代码。我试图让单个列表中的两个对象移动到这些图块,但是当我使用此代码执行此操作时,随机列对象会进入相同的列:

for (int i = 0; i < allCubes.Count; i++)
{
    allCubes[i].transform.position = Vector3.MoveTowards(
        allCubes[i].transform.position,
        allTiles[i].transform.position, 10 * Time.deltaTime);
}

然后想把这两种类型的立方体放在单独的列表中。最终变得更加混乱。哈哈 发布该代码有帮助吗?

【问题讨论】:

  • 你能把你的相关代码贴在这里吗?我们不知道如何修复您的代码。
  • 它到处都有cmets。哈哈让我把它清理一下并粘贴它。
  • 你能澄清你的问题吗?立方体的数量与网格的数量相同吗?上面的代码 sn-p 在什么方面提供了任何随机性?事实上,你想达到什么目标?

标签: c# list unity3d


【解决方案1】:

我知道这是我的一个非常古老的问题。这是一个被取消的项目。我偶然发现了它,出于好奇,我决定尝试完成这个我当时遇到麻烦的特定问题。我做到了。

public Vector3 towerSize = new Vector3(3, 3, 3);

    public GameObject tiles;
    public GameObject randomTile;

//public variables for debugging purposes.
//no real need to be seen in inspector in final.  cleaner too if they're hidden
    public int randomSelectedTile;

    public List<GameObject> allTiles;

    void Start()
    {
        //create grid tower
        for (int x = 0; x < towerSize.x; x++)
        {
            for (int z = 0; z < towerSize.z; z++)
            {
                for (int y = 0; y < towerSize.y; y++)
                {
                    //spawn cubes and space them
                    GameObject obj = (GameObject)Instantiate(tiles);
                    obj.transform.position = new Vector3(x * 1.2f, y * 1.2f, z * 1.2f);

                    //add them all to a List
                    allTiles.Add(obj);
                    obj.name = "tile " + allTiles.Count;
                }
            }
        }

        //select a random cube in the list
        randomSelectedTile = Random.Range(0, allTiles.Count);

        //get the cube object to delete
        GameObject deleteObj = allTiles.ElementAt(randomSelectedTile);
        //spawn the random cube at the position of the cube we will delete
        GameObject rndObj = (GameObject)Instantiate(randomTile);
        rndObj.transform.position = deleteObj.transform.position;

        //remove the element at that location
        allTiles.RemoveAt(randomSelectedTile);
        //insert the random cube at that element's location
        allTiles.Insert(randomSelectedTile, rndObj);

        //destroy the unwanted cube
        Destroy(deleteObj);
    }

很高兴看到随着时间的推移您的进步。以防万一其他人将从该解决方案中受益。再次,我为恢复它而道歉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多