Unity3d Unity 3D中进程的时间延迟
我必须给出进程发生的延迟,这是我在Update函数中调用的。 我也尝试过CoUpdate解决方案。这是我的密码:-Unity3d Unity 3D中进程的时间延迟,unity3d,unityscript,Unity3d,Unityscript,我必须给出进程发生的延迟,这是我在Update函数中调用的。 我也尝试过CoUpdate解决方案。这是我的密码:- function Start() { StartCoroutine("CoStart"); } function CoStart() : IEnumerator { while(true) { yield CoUpdate(); } } function CoUpdate() { //I have placed the code
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.
}
当第三人控制器(另一个对象)移出边界时,我必须移动一个对象。我的代码中包含了“收益率”。但是,出现的问题是:当我在Update()中给出代码时正在移动的对象正在移动,但没有停止。它在上下移动。我不知道发生了什么事!有人能帮忙吗?请,谢谢。我不完全清楚您想要完成什么,但我可以告诉您如何设置协同程序的时间延迟。对于本例,让我们进行简单的冷却,就像您在示例中设置的那样。假设您希望在游戏运行时每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;
}
如果我完全忽略了要点,那么请更新问题,更详细地说明您正试图完成的任务 我不完全清楚您想要实现什么,但我可以向您展示如何为协同程序设置时间延迟。对于本例,让我们进行简单的冷却,就像您在示例中设置的那样。假设您希望在游戏运行时每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;
}
如果我完全忽略了要点,那么请更新问题,更详细地说明您正试图完成的任务 我不建议合作。它有时会使你的电脑崩溃。只需定义一个变量并将其递减即可。例如:
private float seconds = 5;
然后在任何你想延迟的地方执行以下操作:
seconds -= 1 * Time.deltaTime;
if(seconds <= 0) {your code to run}
seconds-=1*Time.deltaTime;
如果(秒我不建议协同程序。它有时会使您的计算机崩溃。只需定义一个变量并减小它。例如:
private float seconds = 5;
然后在任何你想延迟的地方执行以下操作:
seconds -= 1 * Time.deltaTime;
if(seconds <= 0) {your code to run}
seconds-=1*Time.deltaTime;
如果(秒我不是100%确定我理解你的问题,但是如果你想在另一个对象越界时启动一个对象移动,那么只需在第一个对象中引用第二个对象,当第一个对象越界时(在第一个对象的更新中检查此情况)在第二个对象上调用一些公共函数StartMove。我不能100%确定我是否理解您的问题,但是如果您想在另一个对象越界时启动一个对象移动,那么只需在第一个对象中引用第二个对象,并且当第一个对象越界时(在第一个对象的更新中检查此项)在第二个对象上调用一些公共函数StartMove。您应该添加一些有关对象如何移动的信息。到目前为止,我看不到使用协同程序与对象移动之间的关系。顺便说一句,我认为这不是解决问题的正确方法,因为它非常复杂。使用IEnumerator函数,您可以将延迟放在收益率为WaitForSeconds(n)的情况下。您应该添加一些有关对象如何移动的信息。到目前为止,我看不到协同程序的使用与您的对象移动之间的关系。顺便说一句,我认为这不是解决您的问题的正确方法,因为它有多复杂。使用IEnumerator函数,您可以在具有yield WaitForSeconds(n)的情况下设置延迟.Coroutines无法使您的计算机崩溃。Coroutines不会使您的计算机崩溃。Coroutines无法使您的计算机崩溃。Coroutines不会使您的计算机崩溃。