【发布时间】:2014-10-11 02:11:57
【问题描述】:
我的问题是,我希望我的障碍物生成器(位于玩家飞船前面的固定距离处)在每次实例化障碍物时从一组不同的障碍物预制件中随机选择。我发现了很多关于如何随机化位置的线索,但这不是我想要的。我已经看到很多对列表和标签的引用,但我似乎无法弄清楚如何正确实现它们。我将在下面发布我的生成器脚本和我“认为”应该进行更改的 cmets。
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
【问题讨论】:
-
你快到了。查看docs.unity3d.com/Documentation/ScriptReference/… 了解实际生成情况。