gpt4 book ai didi

unity3d - Unity 3D 中进程的时间延迟

转载 作者:行者123 更新时间:2023-12-05 08:13:05 25 4
gpt4 key购买 nike

我必须为该过程的发生提供延迟,我在 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.
}

当第三人称 Controller (这是另一个对象)移出边界时,我必须移动对象。我在我的代码中包含了“yield”。但是,发生的问题是:当我在 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;
}

如果我完全错过了标记,请更新问题并提供有关您试图完成的内容的更多详细信息。

关于unity3d - Unity 3D 中进程的时间延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8707814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com