2

我必须延迟这个过程的发生,我在 Update 函数中调用它。我也尝试过 CoUpdate 解决方法。这是我的代码:-

function Start() 
{
  StartCoroutine("CoStart"); 
} 
function CoStart() : IEnumerator 
{ 
  while(true) 
  { 
    yield CoUpdate(); 
  } 
} 
function CoUpdate() 
{ 
  //I have placed the code of the Update(). 
  //And called the wait function wherever needed. 
} 
function wait() 
{ 
   checkOnce=1; //Whenever the character is moved. 
   yield WaitForSeconds(2); //Delay of 2 seconds. 
}

当第三人称控制器(另一个对象)移出边界时,我必须移动一个对象。我在我的代码中包含了“yield”。但是,发生的问题是:当我在 Update() 中给出代码时正在移动的对象正在移动,但没有停止。它正在上下移动。我不知道发生了什么!有人可以帮忙吗?请,谢谢。

4

3 回答 3

0

我不完全清楚您要完成什么,但我可以向您展示如何为协程设置时间延迟。对于此示例,让我们使用简单的冷却,就像您在示例中设置的一样。假设您想在游戏运行时每 2 秒连续执行一次操作,可以对您的代码进行轻微修改。

function Start()
{
   StartCoroutine(CoStart);
}

function CoStart() : IEnumerator
{
   while(true)
   {
      //.. place your logic here

      // function will sleep for two seconds before starting this loop again
      yield WaitForSeconds(2);   
   }
}

您还可以使用其他一些逻辑计算等待时间

function Start()
{
   StartCoroutine(CoStart);
}

function CoStart() : IEnumerator
{
   while(true)
   {
      //.. place your logic here

      // function will sleep for two seconds before starting this loop again
      yield WaitForSeconds(CalculateWait());   
   }
}

function CalculateWait() : float
{

   // use some logic here to determine the amount of time to wait for the 
   // next CoStart cycle to start
   return someFloat;
}

如果我完全错过了标记,那么请更新问题,详细说明您要完成的工作。

于 2012-01-04T17:38:48.050 回答
0

I am not 100% sure that I understand you question but if you want to start one object to move when the other is out of bound then just make a reference in the first object to the second object and when the first object is out of bounds (check for this in Update of the first object) call some public function StartMove on the second object.

于 2016-10-19T12:35:22.960 回答
-2

我不建议使用 CoRoutines。它有时会使您的计算机崩溃。只需定义一个变量并减少它。例子:

private float seconds = 5;

然后做任何你想延迟的地方:

seconds -= 1 * Time.deltaTime;
if(seconds <= 0) {your code to run}

这将延迟 5 秒。您可以将 5 更改为任何值以更改秒数。您还可以通过更改 1 的值来加快递减速度。(当您想要同步 2 个延迟动作时,这非常有用,通过使用相同的变量)

希望这可以帮助。快乐编码:)

于 2013-10-12T19:54:32.447 回答