Имеется код генерации лабиринта, точнее создание сетки
Синтаксис:
Используется csharp
//Генерация стен по x
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j <= xSize; j++)
{
myPos = new Vector3(initialPos.x + (j * wallLength) - wallLength / 2, 0.0f, initialPos.z + (i * wallLength) - wallLength / 2);
tempWall = Instantiate(wall, myPos, Quaternion.identity) as GameObject;
tempWall.transform.parent = wallHolder.transform;
}
}
//Генерация стен по z
for (int i = 0; i <= ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
myPos = new Vector3(initialPos.x + (j * wallLength), 0.0f, initialPos.z + (i * wallLength) - wallLength);
tempWall = Instantiate(wall, myPos, Quaternion.Euler(0.0f, 90.0f, 0.0f)) as GameObject;
tempWall.transform.parent = wallHolder.transform;
}
}
//пол
for (int i = 0; i <= ySize - 1; i++)
{
for (int j = 0; j < xSize; j++)
{
myPos = new Vector3(initialPos.x + (j * wallLength), -0.425f, initialPos.z + (i * wallLength) - wallLength / 2);
tempFloor = Instantiate(floor, myPos, Quaternion.identity) as GameObject;
tempFloor.transform.parent = wallHolder.transform;
}
}
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j <= xSize; j++)
{
myPos = new Vector3(initialPos.x + (j * wallLength) - wallLength / 2, 0.0f, initialPos.z + (i * wallLength) - wallLength / 2);
tempWall = Instantiate(wall, myPos, Quaternion.identity) as GameObject;
tempWall.transform.parent = wallHolder.transform;
}
}
//Генерация стен по z
for (int i = 0; i <= ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
myPos = new Vector3(initialPos.x + (j * wallLength), 0.0f, initialPos.z + (i * wallLength) - wallLength);
tempWall = Instantiate(wall, myPos, Quaternion.Euler(0.0f, 90.0f, 0.0f)) as GameObject;
tempWall.transform.parent = wallHolder.transform;
}
}
//пол
for (int i = 0; i <= ySize - 1; i++)
{
for (int j = 0; j < xSize; j++)
{
myPos = new Vector3(initialPos.x + (j * wallLength), -0.425f, initialPos.z + (i * wallLength) - wallLength / 2);
tempFloor = Instantiate(floor, myPos, Quaternion.identity) as GameObject;
tempFloor.transform.parent = wallHolder.transform;
}
}
Создавал генератор монет на той же основе:
Синтаксис:
Используется csharp
for (int i = 0; i < ySize; i+=8 )
{
t++;
for (int j = 0; j<=xSize;j+=8)
{
myPos = new Vector3(Random.Range(initialPos.x + (j * wallLength), initialPos.x + (j * wallLength) - wallLength / 2), 0.0f, Random.Range(-(ySize / 2), (ySize / 2)));
tempCoin = Instantiate(coin, myPos, Quaternion.identity) as GameObject;
Debug.Log(myPos);
tempCoin.transform.parent = wallHolder.transform;
}
}
{
t++;
for (int j = 0; j<=xSize;j+=8)
{
myPos = new Vector3(Random.Range(initialPos.x + (j * wallLength), initialPos.x + (j * wallLength) - wallLength / 2), 0.0f, Random.Range(-(ySize / 2), (ySize / 2)));
tempCoin = Instantiate(coin, myPos, Quaternion.identity) as GameObject;
Debug.Log(myPos);
tempCoin.transform.parent = wallHolder.transform;
}
}
Вопрос в следующем, как заспавнить монеты случайным образом, но так, чтобы они появлялись строго посреди комнаты?
Я оперировал следующим, раз пол создается строго внутри каждой комнаты, то изменение значения по X я брал из генератора стен по иксу, а Z удалось приручить значениями размерности комнаты.
После нескольких часов удалось заставить правильно спавниться их по Z, но X гуляет.
Возможно дело в Random.Range, но пока не ясно.
Буду премного благодарен за помощь.