如何延迟 x 秒后开始?

huangapple go评论77阅读模式
英文:

How to delay start for x seconds?

问题

我已经搜索了一下,但似乎找不到答案。我有两个场景,一个有一个播放按钮,点击它会启动下一个场景(游戏),另一个场景是游戏场景。游戏场景中有一个生成器脚本,用于生成随机的障碍物模式。代码如下:

public class Spawner : MonoBehaviour {
    public GameObject[] obstaclePatterns;

    private float timeBtwSpawn;
    public float startTimeBtwSpawn;
    public float decreaseTime;
    public float minTime = 0.55f;

    private void Update()
    {
        if (timeBtwSpawn <= 0)
        {
            int rand = Random.Range(0, obstaclePatterns.Length);    
            Instantiate(obstaclePatterns[rand], transform.position, Quaternion.identity);
            timeBtwSpawn = startTimeBtwSpawn;
            if (startTimeBtwSpawn > minTime) {
                startTimeBtwSpawn -= decreaseTime;
            }
        }
        else {
            timeBtwSpawn -= Time.deltaTime;
        }
    }
}

我想在按下播放按钮并启动游戏后,在生成器开始生成之前等待1秒钟。我不太确定如何做到这一点。任何帮助都将不胜感激。

英文:

I've searched around and couldn't quite find the answer. I have two scenes, one with a play button which starts the next scene (the game) and the other scene is the game. Which has a spawner script which spawns random patterns of obsticles. Which can be seen here.

public class Spawner : MonoBehaviour {
public GameObject[] obstaclePatterns;

private float timeBtwSpawn;
public float startTimeBtwSpawn;
public float decreaseTime;
public float minTime = 0.55f;

private void Update()
{
    if (timeBtwSpawn &lt;= 0)
    {
        int rand = Random.Range(0, obstaclePatterns.Length);    
        Instantiate(obstaclePatterns[rand], transform.position, Quaternion.identity);
        timeBtwSpawn = startTimeBtwSpawn;
        if (startTimeBtwSpawn &gt; minTime) {
            startTimeBtwSpawn -= decreaseTime;
        }
         
    }
    else {
        timeBtwSpawn -= Time.deltaTime;
    }
}}

I would like to after the play button is pressed and the game is started there be a delay for 1 second before the spawner begins spawning. I'm not sure how to do that. Any help would be appreciated.

答案1

得分: 2

你可以直接将Unity的Start函数用作协程。

private bool _canStart;

private IEnumerator Start()
{
    yield return new WaitForSeconds(你想要的时间);
    _canStart = true;
}

private void Update()
{
    if (!_canStart) return;

    你想要的操作
}
英文:

You can use Unity's Start function as a coroutine directly.


private bool _canStart;

private IEnumerator Start()
{
    yield return new WaitForSeconds(whatyouwant);
    _canStart = true;
}

private void Update()
{
    if(!_canStart) return;

    whatyouwant
}

答案2

得分: 0

如果您想要在场景加载后特定时间后启动的例行程序,您可以使用Time.timeSinceLevelLoad这个变量保存了自上次关卡(场景)加载以来的时间(以秒为单位)。
因此,您可以创建一个脚本来激活您的生成器脚本,或者在您的生成器脚本中添加额外的检查。

英文:

if you want to have a routine that starts after a specific amount of time since the scene was loaded you can use Time.timeSinceLevelLoad this variable holds the time in seconds since the last level(scene) was loaded
So you can either create a script that activates your spawner script or add an additional check to your spawner script

答案3

得分: 0

你应该在开始更新生成器之前设置timeBtwSpawn:

timeBtwSpawn = 1; // 秒钟

英文:

You should set timeBtwSpawn before start updating of Spawner:

timeBtwSpawn = 1; // seconds

huangapple
  • 本文由 发表于 2020年1月6日 20:26:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定