Unity3D - 如何随机实例化随机大小的立方体而不重叠?

时间:2014-08-19 23:20:44

标签: random unity3d instantiation overlap cubes

我试图在网格中随机实例化一些随机大小的立方体,但每当我随机实例化所述立方体时,它们往往会重叠。有人能指出我正确的方向,以防止他们重叠?我试过询问团结论坛,到目前为止,没有人愿意回应。

编辑:作为回应,我添加了一些我尝试使用的代码。在大多数情况下,我不知道如何防止立方体重叠。这是在C#中。是的,我意识到我不是世界上最好的编码人员,这就是为什么我会坚持这个问题超过3个星期。

    void InstantiateItems(GameObject[] ItemArray, int Increment)
{
    // Select a random treasure from the appropriate treasure array
    // TreasureSelect is a public int, ItemArray refers to one of three possible different sized arrays passed into this method
    // By calling the random.range and using the passed array's length, you can select a random item from that particular array
    TreasureSelect = Random.Range(0, ItemArray.Length);

    // Store a random x and y position using info from the GridArray
    // Stored Cell is a vector3, GridArray is a 2D array that contains x and y coordinates of a grid's width and height
    // The goal is to store the position of a random dirt cube's position from the grid and use that as the position to instantiate a treasure cube item
    StoredCell = GridArray[Random.Range(0, GridWidth), Random.Range(0, GridHeight)].transform.position;

    // Get the stored x and y offset variables from the treasure cube item's script
    // The offset variables are always half of the treasure cube object's scaled x and y values
    float incomingX = ItemArray[TreasureSelect].GetComponent<Treasure>().offsetX;
    float incomingY = ItemArray[TreasureSelect].GetComponent<Treasure>().offsetY;

    // check to see if the object will spawn partially outside of the dirt field
    // If the randomly generated x value is greater than the grid's width minus the treasure object script's x value, then adjust the randomly generated x value
    if (StoredCell.x >= (float)GridWidth - incomingX)
    {
        // If determined that it will spawn partially off field, adjust the value
        newX = (float)StoredCell.x - incomingX;

        // Check to see if the number is odd or not
        // If the number is odd, then it will need to have an additional .5 added to make the treasure object line up with the dirt cubes above it (since the pivot point is at the center of the unity cube object)
        if(newX % 2 == 1)
        {
            newX += incomingX;
        }
    }
    else if (StoredCell.x < incomingX)
    {
        newX = (float)StoredCell.x + incomingX;
        if (newX % 2 == 1)
        {
            newX -= incomingX;
        }
    }
    else
    {
        newX = StoredCell.x;
        if (newX % 2 == 1)
        {
            newX += incomingX;
        }
    }

    // Now we use the y value instead of the x value and use the grid's height instead of the width
    if (StoredCell.y >= (float)GridHeight - incomingY)
    {
        newY = (float)StoredCell.y - incomingY;
        if (newY % 2 == 1)
        {
            newY += incomingY;
        }
    }
    else if (StoredCell.y < incomingY)
    {
        newY = (float)StoredCell.y + incomingY;
        if (newY % 2 == 1)
        {
            newY -= incomingY;
        }
    }
    else
    {
        newY = StoredCell.y;
        if (newY % 2 == 1)
        {
            newY += incomingY;
        }
    }

    // Instantiate the object with a new vector3 using the spawn point object's rotation
    Instantiate(ItemArray[TreasureSelect], new Vector3(newX, newY, OffSetZ), ItemArray[TreasureSelect].transform.rotation);

    ItemArray[TreasureSelect].transform.position = StoredCell;


}

0 个答案:

没有答案