Question by
kinggamer1235 · Jul 11, 2016 at 10:13 AM ·
c#scripting problemscript.
how to ask if something is not true for an amount of time
basically I want to do this:
if (boolean is not true for one second) { do stuff }
How would i go about doing that?
Thanks in advance!
Comment
Best Answer
Answer by Mindmapfreak · Jul 11, 2016 at 10:22 AM
Do something like
public class BoolOverTime : MonoBehaviour {
private float time;
void Start() {
this.time = 0.0f;
}
void Update() {
if(!yourBoolVariable) { //Replace yourBoolVariable with whatever you want to test
this.time += Time.deltaTime;
}
else {
this.time = 0.0f;
}
if(this.time>=1.0f) {
//Do whatever you want to do if yourBoolVariable was false for >=1s
}
}
}
Answer by ahaykal · Jul 11, 2016 at 10:25 AM
Well you could do a timer to check within this one second. So basically, when b_StartTimer is tru it checks for the boolean.
bool b_CheckWithinOneSecond = false;
float fl_Timer;
bool b_StartTimer = false;
if(b_StartTime)
{
fl_timer += Time.deltaTime;
if(fl_timer <1 && b_CheckWithinOneSecond)
{
// do something
}
if(fl_timer> 1)
{
// Reset Timer
fl_timer = 0;
b_StartTime = false;
}
}