【问题标题】:My IEnumerator Is working but not waiting我的 IEnumerator 正在工作但没有等待
【发布时间】:2021-02-06 13:15:56
【问题描述】:

我正在 Unity 中制作 2d 游戏,并在其中使用此代码实例化敌人

void Update()
{
    StartCoroutine("EnemyInstance");
}
IEnumerator EnemyInstance()
{
   float positionRandoming = Random.Range(1f, 2f);
   if (positionRandoming < 1.5f)
   {
       Instantiate(enemyPrefeb, new Vector3(-4.3f, -1.45f, 1f), position1.rotation, transform.parent);
       enemyScript.pos = 1;
   }
   if (positionRandoming >= 1.5f)
   {
       Instantiate(enemyPrefeb, new Vector3(3.6f, -1.45f, 1f), position2.rotation, transform.parent);
       enemyScript.pos = 2;
   }
    yield return new WaitForSeconds(2.4f);
}

在这段代码中,IEnumerator 方法正在执行它们的工作,但不产生返回新的 WaitForSeconds。意味着如果我在 Unity 中运行它,敌人会在每一帧中实例化。 我该如何解决?

【问题讨论】:

    标签: c# unity3d ienumerator


    【解决方案1】:

    我不是 Unity 开发者,但我认为有两个问题:

    1. 您从经常调用的 Update 调用它 - 所以每次,您都在重新启动协程
    2. 你没有循环 - 所以你的代码基本上只运行一次

    我怀疑你想从Start而不是Update调用它,并在方法中放置一个循环:

    IEnumerator EnemyInstance()
    {
       while (true)
       {
           float positionRandoming = Random.Range(1f, 2f);
           if (positionRandoming < 1.5f)
           {
               Instantiate(enemyPrefeb, new Vector3(-4.3f, -1.45f, 1f), position1.rotation, transform.parent);
               enemyScript.pos = 1;
           }
           if (positionRandoming >= 1.5f)
           {
               Instantiate(enemyPrefeb, new Vector3(3.6f, -1.45f, 1f), position2.rotation, transform.parent);
               enemyScript.pos = 2;
           }
           yield return new WaitForSeconds(2.4f);
       }
    }
    

    【讨论】:

      【解决方案2】:

      每次调用更新函数都会启动一个新的协程。

      您可以添加一个布尔值来检查协程当前是否正在运行。

      private bool spawningEnemy = false;
      
      void Update()
      {
         if(!spawningEnemy) {
            spawningEnemy = true;
            StartCoroutine("EnemyInstance");  
         }
      
      }
      
      IEnumerator EnemyInstance()
      {
         float positionRandoming = Random.Range(1f, 2f);
         if (positionRandoming < 1.5f)
         {
              Instantiate(enemyPrefeb, new Vector3(-4.3f, -1.45f, 1f), position1.rotation, transform.parent);
              enemyScript.pos = 1;
          }
          if (positionRandoming >= 1.5f)
          {
              Instantiate(enemyPrefeb, new Vector3(3.6f, -1.45f, 1f), position2.rotation, transform.parent);
              enemyScript.pos = 2;
         }
          yield return new WaitForSeconds(2.4f);
          spawningEnemy = false;
      }
      

      【讨论】:

        【解决方案3】:

        你想每 2.4 秒生成一次敌人吗?

        yield return new WaitForSeconds(2.4f); 上面的代码在每一帧立即运行,无需任何等待,下面的代码等待 2.4 秒,在您的情况下为空。 把你的代码放在它下面,你就可以开始了。

        void Update()
        {
            StartCoroutine("EnemyInstance");
        }
        
        IEnumerator EnemyInstance()
        {
           yield return new WaitForSeconds(2.4f);
        
           float positionRandoming = Random.Range(1f, 2f);
           if (positionRandoming < 1.5f)
           {
               Instantiate(enemyPrefeb, new Vector3(-4.3f, -1.45f, 1f), position1.rotation, transform.parent);
               enemyScript.pos = 1;
           }
           if (positionRandoming >= 1.5f)
           {
               Instantiate(enemyPrefeb, new Vector3(3.6f, -1.45f, 1f), position2.rotation, transform.parent);
               enemyScript.pos = 2;
           }
        }
        

        【讨论】:

          猜你喜欢
          • 2012-08-15
          • 2021-12-07
          • 1970-01-01
          • 2015-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-12
          相关资源
          最近更新 更多