[英]Unity C# How to check IF (statement) is true for n amount of time?
I have an If
statement that returns true or false, naturally.我有一个If
语句,它自然地返回 true 或 false。 How do I check the condition for n
amount of time (frames, seconds)?如何检查n
时间(帧、秒)的条件? I need the bool to return false only if condition is true for half a second.仅当条件为真半秒钟时,我才需要布尔返回假。
Put some code in your Update method and use a float member variable and add Time.deltaTime.在 Update 方法中添加一些代码并使用浮点成员变量并添加 Time.deltaTime。 The update method triggers every frame.更新方法触发每一帧。 Once the variable is over 0.5f do your magic.一旦变量超过 0.5f,就可以发挥你的作用。
public class MyScript : MonoBehaviour
{
float timer:
void Update()
{
// If condition is false, reset timer and return out. Replace condition with your logic.
if (!condition)
{
timer = 0.0f;
return;
}
timer += Time.deltaTime:
if (timer > 0.5f)
{
// do stuff
timer = timer - 0.5f; // reset timer
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.