gpt4 book ai didi

c# - n 秒后注视触发事件 - Unity

转载 作者:太空宇宙 更新时间:2023-11-03 20:53:58 25 4
gpt4 key购买 nike

我有一个“指针输入”事件,但我只希望在注视对象上事件 n 秒时触发该事件。

public void PointerEnter() {
// change scene if the gaze point have been active for n seconds.
}

无论如何要实现这个目标?

仅仅有一个超时是行不通的,因为它仍然会执行,而不是指针是否锁定在对象上。

最佳答案

您可以使用 bool 变量来保持指针何时进入和退出的状态,方法是将其设置为 true 和 false。然后可以在 Update 函数中检查此 bool 变量。当它为 true 时,启动一个计时器,如果在计时器期间变为 false,则将计时器重置为 0。检查计时器何时超过 x 秒然后加载新场景。

下面的示例假设当指针指向时调用 PointerEnter,当指针不再指向时调用 PointerExit。函数可能会有所不同,具体取决于您使用的 VR 插件,但其余代码是相同的。

const float nSecond = 2f;

float timer = 0;
bool entered = false;

public void PointerEnter()
{
entered = true;
}

public void PointerExit()
{
entered = false;
}

void Update()
{
//If pointer is pointing on the object, start the timer
if (entered)
{
//Increment timer
timer += Time.deltaTime;

//Load scene if counter has reached the nSecond
if (timer > nSecond)
{
SceneManager.LoadScene("SceneName");
}
}
else
{
//Reset timer when it's no longer pointing
timer = 0;
}
}

关于c# - n 秒后注视触发事件 - Unity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442219/

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