【问题标题】:Spawn random game object?生成随机游戏对象?
【发布时间】: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

【问题讨论】:

标签: c# unity3d


【解决方案1】:

到目前为止,您所拥有的一切看起来都还不错。将一个公共数组附加到您的单一行为中,您可以从检查器中拖动预制件,您可以使用它来生成

在你的方法“SpawnWall()”中,你只需要从你的数组中选择一个预制件

GameObject randPrefab = spawnObject[spawnObjectIndex];

然后你会使用

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

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

我建议将您的数组重命名为“obstaclePrefabs”之类的名称,因为“spawnObject”并不能真正描述要生成的障碍物列表。

【讨论】:

    【解决方案2】:

    在运行时加载游戏对象的另一种方法是将项目放置在名为“资源”的文件夹中,然后使用下面的调用:

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

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

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

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

    【讨论】:

      【解决方案3】:

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

      使用 UnityEngine;

      公共类生成: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;
          }
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2021-02-06
        • 2023-04-05
        • 2021-04-19
        • 1970-01-01
        • 2016-11-19
        • 1970-01-01
        • 1970-01-01
        • 2016-04-13
        • 2020-09-23
        相关资源
        最近更新 更多