Check if something just pass through an object or stay some fixed time on it
Hello guys, i'm new on it and my English is not so good, so be nice! Please :)
what i want to do is:
if the player pass trough an object, nothing happens! but if it stays on it for example 2 seconds, something happens.
i'm using onTriggerEnter2D(), onTriggerExit2D and coroutine. but still not working, i pass trough the object and 2 seconds later it does the hellthing.
Thank you so much!!
PS: I don't want that the player needs to stop on it. it needs be free to walk on the trigger, but if he gets out until some defined time, nothing happens!
Hello.
This is veryunespecyfic... We need the code, or know what is happening.. is executing the OnTRiggerEnter ? Is executing the corutine? is yieling correctly? how does it finish...
CODEE!!!
Answer by LeroFelipe · Feb 17, 2020 at 04:24 AM
[RESOLVED]
Coroutine lastRoutine = null;
private void OnTriggerEnter2D(Collider2D trigger2D)
{
if (trigger2D.gameObject.CompareTag("Something"))
{
print("On it!!!");
lastRoutine = StartCoroutine(WaitToDo());
}
}
IEnumerator WaitToDo()
{
yield return new WaitForSeconds(5.0f); // wait 5 seconds to "Do Something"
print("Do Something");
}
private void OnTriggerExit2D(Collider2D trigger2D)
{
if (trigger2D.gameObject.CompareTag("Somethign"))
{
StopCoroutine(lastRoutine);
}
}
Your answer
Follow this Question
Related Questions
What is Coroutine, Yiel and IENumerator? 1 Answer
Hey guys I need help with scripting a 2 player car game I'm very confused on the whole process. 0 Answers
WaitForSeconds in Coroutines do not work. 0 Answers
How to make a scrip stop a coroutine from another script? 0 Answers
Using coroutine and texture array to randomly associate textures? 1 Answer