基本概念:
- 从障碍物
- 创建一个预制与一对夫妇的参数(每面墙,可能的位置之间的距离等),创建一个脚本(如WallSpawner),并将其连接到在你的场景中的一个对象(例如你的案例
Walls
)。
- 在
Start
或Awake
方法中,使用Instantiate
创建预制件的副本并传入随机选取的位置。
示例脚本:
public class WallSpawner : MonoBehaviour
{
// Prefab
public GameObject ObstaclePrefab;
// Origin point (first row, first obstacle)
public Vector3 Origin;
// "distance" between two rows
public Vector3 VectorPerRow;
// "distance" between two obstacles (wall segments)
public Vector3 VectorPerObstacle;
// How many rows to spawn
public int RowsToSpawn;
// How many obstacles per row (including the one we skip for the gap)
public int ObstaclesPerRow;
void Start()
{
Random r = new Random();
// loop through all rows
for (int row = 0; row < RowsToSpawn; row++)
{
// randomly select a location for the gap
int gap = r.Next(ObstaclesPerRow);
for (int column = 0; column < ObstaclesPerRow; column++)
{
if (column == gap) continue;
// calculate position
Vector3 spawnPosition = Origin + (VectorPerRow * row) + (VectorPerObstacle * column);
// create new obstacle
GameObject newObstacle = Instantiate(ObstaclePrefab, spawnPosition, Quaternion.identity);
// attach it to the current game object
newObstacle.transform.parent = transform;
}
}
}
}
示例参数:
实施例的结果:
感谢。你不知道这对我有多大。对于由此造成的不便,我们深表歉意,但我非常感谢您的帮助。再次谢谢你!我现在感觉非常愚蠢。 –
@PascalGamingYTPascalPlayz没问题。如果这回答你的问题,请点击我问题的左上角的复选标记,将其标记为答案并关闭问题。 –