生成随机游戏对象?

4
我的问题是,我希望我的障碍物生成器,在玩家的飞船前面固定距离处,每次实例化障碍物时从不同的预制件中随机选择。我已经找到很多关于如何随机位置的线程,但这不是我要找的。我看到很多引用列表和标签的参考资料,但似乎无法正确地实现它们。我将在下面发布我的生成器脚本,并在其中添加注释提示需要更改的地方。
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

你已经接近成功了。请查看http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html以获取实际生成的代码。 - Bart
3个回答

3

你目前的代码看起来很好。将公共数组附加到您的Monobehaviour上,可以让您从检查器中拖动预制体,您可以使用它们来生成。

在你的方法'SpawnWall()'中,你只需要从数组中选择一个预制体。

GameObject randPrefab = spawnObject[spawnObjectIndex];

那么你需要使用


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

通过其转换,您可以执行任何位置代码。

我建议将数组重命名为“obstaclePrefabs”,因为“spawnObject”不能很好地描述要生成的障碍列表。


1
使用随机数生成器。为每个障碍分配一个“案例”,并在每个案例中告诉它该做什么。在我的脚本中,我需要让各种平台以随机的方式出现,但在一定时间间隔内。

使用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;
    }


}

1
在运行时加载GameObject的另一种方法是将物体放置在名为“Resources”的文件夹中,然后使用以下调用:
GameObject obstacle = Resources.Load("myGameObject") as GameObject;

如果项目在资源文件夹内的文件夹中,则只需调用以下内容:
GameObject obstacle = Resources.Load(@"myFolder/myGameObject") as GameObject;

请注意,使用此方法时,物品生成时会有稍微的延迟,因为它需要被加载到游戏中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接