我刚刚开始使用Unity,我想构建以下函数: 目前我按下一个按钮,在屏幕外产生一颗小行星,然后小行星从右向左穿过屏幕,最后我在屏幕外摧毁小行星.所有这些都已经奏效了.但现在我想改变按钮功能,这样它就可以延迟几秒钟产生更多小行星.我不知道该怎么做.我的代码目前如下所示:
public class ButtonSkript : MonoBehaviour
{
private Vector3 spawnPosition = new Vector3(15, 0, 0);
public GameObject planet_001Prefab;
public GameObject planet_011Prefab; // not in use
public GameObject planet_017Prefab; // not in use
public GameObject planet_079Prefab; // not in use
public void ShowJourney()
{
int[] asteroids_array = {27, 569, 1293, 37};
int[] material_type_array = {0, 2, 1, 3}; // not in use
for (int i = 0; i < asteroids_array.Length; i++) {
GameObject a = Instantiate(planet_001Prefab);
a.transform.position = spawnPosition;
Destroy(a, 3);
// wait for 3 seconds
}
}
}
public class Translate : MonoBehaviour
{
public float speed = 10.0f;
// Update is called once per frame
void Update()
{
moveAsteroid(new Vector2(-1, 0));
}
void moveAsteroid(Vector2 direction)
{
transform.Translate(direction * speed * Time.deltaTime);
}
}
我已经try 了System.Threading.Thread.Sleep(3000);
,我还try 用IEnumarate
和yield return new waitForSeconds(3.0f);
来构建一个计时器,但这两个东西都不起作用,或者我没有正确地实现它.
这可能是一个简单的问题,但我自己想不出来.
感谢您的帮助:)