- Home /
wait a second before say path is valid?
hey folks! i want to set a bool to true as soon as a navmesh agent finds a valid path. this works fine but he sould wait some seconds before set the bool to true cause you might want to change something in the last seconds. currently it looks like this:
void CheckPath()
{
agent = GetComponent<NavMeshAgent>();
NavMeshPath path = new NavMeshPath();
agent.CalculatePath(target.position, path);
if (path.status == NavMeshPathStatus.PathComplete)
{
//wait 3 seconds
Path =true;
Debug.Log("PathComplete");
}
else
{
Path =false;
//set timer back to 3 seconds
Debug.Log("PathInvalid");
}
i know i could use some co routines or IEnumerator but i have no clue how i could use it in the sample above? can somebody help me please? thanks!
Answer by Jessespike · Dec 15, 2015 at 05:44 PM
Change the void to a IEnumerator type:
//void CheckPatch() {
IEnumerator CheckPath() {
When you want a delay, instruct the coroutine to wait:
yield return new WaitForSeconds(3f);
Also instead of calling CheckPath() directly, it will need to be called with StartCoroutine()
StartCoroutine(CheckPath());
So the yield will count down from 3 on. And. What will happen when the path turns false again during this 3 seconds?
Answer by LordDarkon76 · Dec 15, 2015 at 10:04 PM
http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
if....
StartCoroutine(WaitPath(3.0F));
...
IEnumerator WaitPath(float waitTime)
{
yield return new WaitForSeconds(waitTime);
Path =true;
}
Your answer
Follow this Question
Related Questions
Need Help with a Null Reference 1 Answer
Can't get past WaitForSeconds in my coroutine 1 Answer
Coroutine doesn't work when called from another method. 3 Answers
Delay after input? 1 Answer
How to stop coroutines, when paused 2 Answers