简体   繁体   English

如何在Unity中的特定时间启动和停止生成器

[英]How to start and stop spawners at specific times in Unity

I am trying to make an endless 2D game. 我正在尝试制作无尽的2D游戏。 I want to use 3-4 spawners for my game. 我想在游戏中使用3-4个生成器。 My problem is that I can't set a specific time for my spawners. 我的问题是我无法为我的生成器设置特定的时间。

After game started 游戏开始后
First spawner will start spawning after 5 seconds and stops after 15 seconds, and it never turns on again. 第一个生成器将在5秒后开始生成,并在15秒后停止,并且永远不会再次打开。

Second spawner will start spawning after 15 seconds and stop after 25 seconds, and it never turns on again. 第二个生成器将在15秒后开始生成,并在25秒后停止,并且永远不会再次打开。

Third spawner will start spawning after 25 seconds and stop after 40 seconds, and it never turns on again. 第三个生成器将在25秒后开始生成,并在40秒后停止,并且永远不会再次打开。

I would like to use the same spawner script for all of them. 我想对所有人使用相同的生成器脚本。 I think I need some public values. 我认为我需要一些公共价值观。

Here is my spawner code : 这是我的生成器代码:

using UnityEngine;
using System.Collections;    

public class RandomSpawner : MonoBehaviour
{
    bool isSpawning = false;
    public float minTime = 5.0f;
    public float maxTime = 15.0f;
    public Transform[] spawnPoints;
    public GameObject[] enemies;  

    IEnumerator SpawnObject(int index, float seconds)
    {
        Debug.Log("Waiting for " + seconds + " seconds");
        int spawnPointIndex = Random.Range(0, spawnPoints.Length);

        yield return new WaitForSeconds(seconds);
        Instantiate(enemies[index], spawnPoints[spawnPointIndex].position, transform.rotation);

        isSpawning = false;            
    }

    void Update()
    {
        if (!isSpawning)
        {
            isSpawning = true; 
            int konumIndex = Random.Range(0, enemies.Length);
            StartCoroutine(SpawnObject(konumIndex, Random.Range(minTime, maxTime)));
        }
    }
}

Use In-Built InvokeRepeating Method,You need to edit according to your Game. 使用内置的InvokeRepeating方法,您需要根据自己的游戏进行编辑。 https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html

One option I have used for this kind of thing is to test against Time.realtimeSinceStartup which gives you a the number of seconds since the game started: 我用于此类事情的一个选项是针对Time.realtimeSinceStartup进行测试,该时间为您提供自游戏开始以来的秒数:

if (Time.realtimeSinceStartup >= minTime && Time.realtimeSinceStartup <= maxTime) {
    //spawn stuff
}

That might be enough, but one extra thing to mention is if the start of your game is not exactly the correct reference point (for example your scene with the spawner created might start long after the game starts if you have a main menu first), then you might need to store a reference time for when the spawner is...uh...spawned. 那可能就足够了,但还有一件事要提到的是,如果游戏的开始不是完全正确的参考点(例如,如果您首先拥有一个主菜单,则具有创建的生成器的场景可能会在游戏开始很长时间之后开始),那么您可能需要存储何时生成... uh ...生成参考时间。 eg: 例如:

public class RandomSpawner : MonoBehaviour {
    float objectStartTime;
    void Start() {
        objectStartTime = Time.realtimeSinceStartup;
    }

    void Update() {
        var timeSinceObjectStart = Time.realtimeSinceStartup - objectStartTime;
        if (timeSinceObjectStart > minTime && timeSinceObjectStart < maxTime) {
        //spawn stuff
         }
    }
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM