我的问题是,我希望我的障碍物生成器,在玩家的飞船前面固定距离处,每次实例化障碍物时从不同的预制件中随机选择。我已经找到很多关于如何随机位置的线程,但这不是我要找的。我看到很多引用列表和标签的参考资料,但似乎无法正确地实现它们。我将在下面发布我的生成器脚本,并在其中添加注释提示需要更改的地方。
using UnityEngine;
using System.Collections;
public class RandomSpawner : MonoBehaviour
{
public GameObject[] spawnObject; //somehow change this to incorporate multiple gameobject prefabs, will an array support that?
//Would I create public variables for each prefab I want to be randomly chosen from, or would those be contained in the array above?
public float xRange = 1.0f;
public float yRange = 1.0f;
public float minSpawnTime = 1.0f;
public float maxSpawnTime = 10.0f;
void Start()
{
Invoke("SpawnWall", Random.Range(minSpawnTime,maxSpawnTime));
}
void SpawnWall()
{
float xOffset = Random.Range(-xRange, xRange);
float yOffset = Random.Range(-yRange, yRange);
int spawnObjectIndex = Random.Range(0,spawnObject.Length);
//above line will have to change to reflect whatever goes above Start, possibly below as well