- Home /
How would i make it so you have to wait a certain amount of time In the trigger for OnTriggerEnter to activate
Hi All, I was messing around for a bit and i was just wondering how you would make it so that when an object enters a trigger it has to wait there for five seconds or so for it to activate. private void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "L_O_S") { Debug.Log("Object found"); Destroy(gameObject); } } I have looked at waitforseconds, invoke and more to try and achieve this but it all just activates it a couple of seconds after you enter the trigger, not making you wait in the trigger for those seconds.
Answer by eneIr · May 14, 2019 at 04:26 AM
I think you can use Time.deltaTime, for example: bool isTriggered;
float waitTime = 5f; void Update () { if (isTriggered == true) {waitTime -= Time.deltaTime;} if (isTriggered == true && waitTime <= 0f) {Debug.Log("Object found"); Destroy(GameObject.FindGameObjectWithTag("L_O_S"));} }
private void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "L_O_S") { isTriggered = true; } }
this does exactly what I want it to do, I have an issue though, I am using a flashlight cone object where you have to shine it on an object for an extended period of time to identify the object and collect it, but the flashlight object gets turned off when not in use. I have fumbled around a bit and discovered that ontriggerExit is not equipped to deal with this and doesn't see the object collider being turned off as the collider exiting the trigger.
Also, thank you for your previous answer it helped a lot :)
Your answer
Follow this Question
Related Questions
getComponent not working with Triggers? 1 Answer
Sound On Collision Not Working 3 Answers
Yield/OnTriggerEnter Help C# 1 Answer