- Home /
How to make boolean be true for x amount of seconds?
Hello!
I'd like to make so that when the player enters the trigger a boolean will become true and then the boolean becomes false after 3 seconds. How do I do that?
Thanks in advance!
Comment
Best Answer
Answer by $$anonymous$$ · May 05, 2015 at 08:25 PM
You can use Invoke Method like so:
private void OnTriggerEnter(Collider other)
{
YourBool = true;
Invoke("SetBoolBack", YOUR_ELAPSED_TIME);
}
private void SetBoolBack()
{
YourBool = false;
}
Answer by Cherno · May 05, 2015 at 08:20 PM
public bool myBool = false;
public bool touchingTrigger = false;
void OnTriggerEnter(Collider other) {
myBool = true;
StopCoroutine(("SetMyBoolToFalse");
StartCoroutine("SetMyBoolToFalse");
}
void OnTriggerStay(Collider other) {
touchingTrigger = true;
}
void OnTriggerExit(Collider other) {
touchingTrigger = false;
}
private IEnumerator SetMyBoolToFalse() {
yield return new WaitForSeconds(3f);
if(touchingTrigger == false) {
myBool = false;
}
yield return null;
}
man i am so so so grateful like omg !!! i am so happy right now you just solved something i couldn't fix for 2 weeks pff i am just so grateful if you need anything hit me up ;)