1

グリッド内のランダムなサイズのキューブをランダムにインスタンス化しようとしていますが、キューブをランダムにインスタンス化するたびに、重複する傾向があります。誰かが私を正しい方向に向けて、それらが重ならないようにすることはできますか? Unity フォーラムに質問してみましたが、これまでのところ、だれも回答を求めていません。

編集:それに応じて、使用しようとしているコードの一部を追加しました。ほとんどの場合、立方体が重ならないようにする方法はわかりません。これは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;


}
4

0 に答える 0