gpt4 book ai didi

c# - 触发器激活后如何等待 5 秒?

转载 作者:太空狗 更新时间:2023-10-30 00:40:22 26 4
gpt4 key购买 nike

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

我尝试过的

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

}
}

最佳答案

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

}
}

关于c# - 触发器激活后如何等待 5 秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28404808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com