【问题标题】:How to spawn objects at random times?如何随机生成对象?
【发布时间】:2015-12-02 13:03:42
【问题描述】:

我正在尝试创建一个随机生成对象的脚本,不幸的是我的脚本运行得不是很好。你能帮我随机化游戏对象生成之间的时间吗?谢谢!

#pragma strict

var SpawnObject : GameObject;
var SpawnPoint : GameObject;

var SpawnCounter : int = 0;
var SpawnCounterMinMax : int =0;
var SpawnCounterMaxMax : int =0;


function Update () 
{
    var float_min_bother_I_hate_you_js : float = this.SpawnCounterMinMax;
    var float_max_bother_I_hate_you_js : float = this.SpawnCounterMaxMax;
    var SpawnCounterMax = Random.Range(float_min_bother_I_hate_you_js, float_max_bother_I_hate_you_js);
    this.SpawnCounter++;
    if (this.SpawnCounter >= SpawnCounterMax )
    {
        Instantiate(this.SpawnObject, this.SpawnPoint.transform.position, this.SpawnPoint.transform.rotation );
        this.SpawnCounter = 0;
        SpawnCounterMax = Random.Range(float_min_bother_I_hate_you_js, float_max_bother_I_hate_you_js);
    }

}

【问题讨论】:

    标签: random unity3d scripting unityscript


    【解决方案1】:

    如果您希望能够指定最小/最大随机时间范围,这样的方法应该可以:

     #pragma strict
    
    var SpawnObject : GameObject;
    var SpawnPoint : GameObject;
    
    var NextSpawnTime : float = 0;
    var MinSpawnTime : float =0;
    var MaxSpawnTime : float =0;
    
    function SetTimer()
    {
        this.NextSpawnTime = Random.Range(this.MinSpawnTime, this.MaxSpawnTime);
    }
    
    function Start ()
    {
        //initialise the spawn counter at startup
        this.SetTimer();
    }
    
    function Update () 
    {
        this.NextSpawnTime -= Time.deltaTime;
        if(this.NextSpawnTime <= 0)
        {
              Instantiate(this.SpawnObject, this.SpawnPoint.transform.position, this.SpawnPoint.transform.rotation );
              this.SetTimer();
        }    
    }
    

    使用 Time.deltaTime 来减少一个以秒为单位存储某个值的计时器,以确保无论帧速率如何,您的生成都发生在给定范围内。

    进一步阐明为什么您可能会在代码中看到不需要的行为。您在每次更新时重新随机化 SpawnCounter 的最大值,而不是在每次生成对象时设置一次。

    【讨论】:

      【解决方案2】:

      您可以使用协程来实现这一点,就像这样。

      int numberOfObjectsToCreate = 5; // number of objects you want to spawn.
      float minTimeDiff = 1.0f; // minimum time difference between 2 objects spawned.
      float maxTimeDiff = 5.0f; // Maximam time difference between 2 spawns.
      
      public GameObject ObjectToSpawn; // object that is to be spawned;
      
      void Start() {
          StartCoroutine(CreateObjectsAtRandom());
      }
      
      IEnumerator CreateObjectsAtRandom() {
              for(int i = 0; i < numberOfObjectsToCreate; i++) {
                  GameObject obj = Instantiate(ObjectToSpawn, Vector3.zero, Quarternion.identity) as GameObject;
                  yield return new WaitForSeconds(Random.Range(minTimeDiff, maxTimeDiff)); // wait for a random time before spawning the next object.
              }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-05-13
        • 2019-08-02
        • 1970-01-01
        • 2015-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多