触发器激活后如何等待5秒钟?

hue*_*nny 4 c# triggers unity-game-engine

我试图在触发器满足后等待五秒钟,然后在五秒钟后我想要进入下一个场景.问题是一旦触发得到满足,它就会自动进入下一个场景.

我试过了什么

using UnityEngine;
using System.Collections;

public class DestroyerScript : MonoBehaviour {


IEnumerator WaitAndDie()
{
    yield return new WaitForSeconds(5);

}
void Update()
{

        StartCoroutine(WaitAndDie());         

}
void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player") 
    {
        Update();     
        Application.LoadLevel("GameOverScene");
        return;
    }

}
}
Run Code Online (Sandbox Code Playgroud)

我也试过了

using UnityEngine;
using System.Collections;

public class DestroyerScript : MonoBehaviour {


IEnumerator WaitAndDie()
{
    yield return new WaitForSeconds(5);

}

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player") 
    {
        StartCoroutine(WaitAndDie());         
        Application.LoadLevel("GameOverScene");
        return;
    }

}
}
Run Code Online (Sandbox Code Playgroud)

小智 5

只有调用Application.LoadLevel后yield return:).

IEnumerator WaitAndDie()
{
    yield return new WaitForSeconds(5);
    Application.LoadLevel("GameOverScene");
}

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player") 
    {
        StartCoroutine(WaitAndDie());         
        return;
    }

}
}
Run Code Online (Sandbox Code Playgroud)