2015-02-09 174 views
4

我试图等到触发器满足后五秒钟,然后在五秒钟后,我想去下一个场景。问题是,一旦触发器被满足,它会自动进入下一个场景。如何在激活触发器后等待5秒钟?

我已经试过

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; 
    } 

} 
} 

我也曾尝试

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; 
    } 

} 
} 

回答

2

这应该工作

using UnityEngine; 
using System.Collections; 

public class DestroyerScript : MonoBehaviour { 


bool dead; 

IEnumerator OnTriggerEnter2D(Collider2D other) 
{ 
    if (other.tag == "Player") 
    { 
     yield return new WaitForSeconds(5); 
     Application.LoadLevel("GameOverScene"); 
     dead = true; 
     return dead; 

    } 

} 
} 
5

只有yield return后打电话Application.LoadLevel :)。

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

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

} 
}