Unity3D - Как случайным образом создать кубы случайного размера без перекрытия?

Я пытаюсь случайным образом создать несколько кубов случайного размера в сетке, но всякий раз, когда я создаю случайный экземпляр указанных кубов, они имеют тенденцию перекрываться. Может ли кто-нибудь указать мне правильное направление, чтобы они не перекрывались? Я пытался задать вопрос на форумах единства и пока никто не удосужился ответить.

Изменить: В ответ я добавил часть кода, который я пытаюсь использовать. По большей части, я понятия не имею, как предотвратить перекрытие кубов. Это в 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 ответов

Другие вопросы по тегам