产生随机游戏对象?

时间:2013-12-01 21:34:21

标签: c# unity3d

我的问题是,我希望我的障碍物产生器,即在玩家宇宙飞船前方的一定距离,每次实例化一个障碍物时,从一组不同的障碍物预制件中随机选择。我已经找到了很多关于如何随机化位置的线索,但这不是我正在寻找的。我已经看到很多对列表和标签的引用,但我似乎无法弄清楚如何正确实现它们。我将在下面发布我的spawner脚本,其中包含我认为应该进行更改的注释。

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

3 个答案:

答案 0 :(得分:3)

到目前为止你看起来很好看。将公共阵列连接到monobehaviour将允许您从可用于生成的检查器中拖动预制件

在您的方法'SpawnWall()'中,您只需从阵列中选择一个预制件

GameObject randPrefab = spawnObject[spawnObjectIndex];

然后你会用

GameObject newObstacle = GameObject.Instantiate(randPrefab) as GameObject;

通过变换做任何你想要的位置代码

我建议将数组重命名为'obstaclePrefabs',因为'spawnObject'并没有真正描述产生的障碍列表。

答案 1 :(得分:1)

在运行时加载GameObjects的另一种方法是将项目放在名为“Resources”的文件夹中,然后使用下面的调用:

GameObject obstacle = Resources.Load("myGameObject") as GameObject;

如果项目位于Resources文件夹内的文件夹内,则只需调用:

GameObject obstacle = Resources.Load(@"myFolder/myGameObject") as GameObject;

请注意,使用此方法时,项目的生成会在加载到游戏中时稍有延迟。

答案 2 :(得分:1)

使用随机数生成器。为每个障碍物分配一个" case"并在每个案例中告诉它该做什么。在我的脚本中,我需要各种平台随机出现,但是按照设定的间隔。

使用UnityEngine;

public class Generate:MonoBehaviour {     public GameObject prefab1;

public GameObject prefab2;

public GameObject prefab3;

public GameObject prefab4;

public GameObject prefab5;

public GameObject prefab6;

public int platform;

// Use this for initialization
void Start()

{
    InvokeRepeating("CreateObstacle", 1f, 1.5f);    //generate
}

void CreateObstacle()
{
    platform = Random.Range (1, 7);             //radom number generator b/w 1 and 7
    float randomY = Random.Range(-5f, 5f);      // appear b/w -5 and 5 in y-axis
    float rightScreenBound = 10;                // spawn this much right of the screen

    switch (platform)
    {
    case 1: 
        Instantiate(prefab1, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity); 
        break;
    case 2:
        Instantiate(prefab2, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
        break;
    case 3: 
        Instantiate(prefab3, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
        break;
    case 4: 
        Instantiate(prefab4, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
        break;
    case 5: 
        Instantiate(prefab5, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
        break;
    case 6: 
        Instantiate(prefab6, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
        break;
    }


}