首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在继续统一的波浪之前,我该如何停下来?

在继续统一的波浪之前,我该如何停下来?
EN

Stack Overflow用户
提问于 2022-04-03 20:16:31
回答 2查看 50关注 0票数 0

我正在制作一个基于波浪的游戏,它运行得很好,但是我想让它在它继续到下一波之前,玩家必须按下一个键才能让游戏知道他们已经准备好了,我之所以想这么做是因为我想要实现一家商店来购买海浪后的升级,这是我的wave脚本:

SpawnManager.cs

代码语言:javascript
复制
using System.Collections;
using UnityEngine;

[System.Serializable]
public class Wave
{
    public int EnemiesPerWave;
    public GameObject Enemy;
}

public class SpawnManager : MonoBehaviour
{
    public Wave[] Waves; // class to hold information per wave
    public Transform[] SpawnPoints;
    public float TimeBetweenEnemies = 2f;

    private int _totalEnemiesInCurrentWave;
    private int _enemiesInWaveLeft;
    private int _spawnedEnemies;

    private int _currentWave;
    private int _totalWaves;

    void Start ()
    {
        _currentWave = -1; // avoid off by 1
        _totalWaves = Waves.Length - 1; // adjust, because we're using 0 index

        StartNextWave();
    }

    void StartNextWave()
    {
        _currentWave++;

        // win
        if (_currentWave > _totalWaves)
        {
            return;
        }

        _totalEnemiesInCurrentWave = Waves[_currentWave].EnemiesPerWave;
        _enemiesInWaveLeft = 0;
        _spawnedEnemies = 0;

        StartCoroutine(SpawnEnemies());
    }

    // Coroutine to spawn all of our enemies
    IEnumerator SpawnEnemies()
    {
        GameObject enemy = Waves[_currentWave].Enemy;
        while (_spawnedEnemies < _totalEnemiesInCurrentWave)
        {
            _spawnedEnemies++;
            _enemiesInWaveLeft++;

            int spawnPointIndex = Random.Range(0, SpawnPoints.Length);

            // Create an instance of the enemy prefab at the randomly selected spawn point's 
            // position and rotation.
            Instantiate(enemy, SpawnPoints[spawnPointIndex].position, 
                             SpawnPoints[spawnPointIndex].rotation);
            yield return new WaitForSeconds(TimeBetweenEnemies);
        }
        yield return null;
    }
    
    // called by an enemy when they're defeated
    public void EnemyDefeated()
    {
        _enemiesInWaveLeft--;
        
        // We start the next wave once we have spawned and defeated them all
        if (_enemiesInWaveLeft == 0 && _spawnedEnemies == _totalEnemiesInCurrentWave)
        {
            StartNextWave();
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2022-04-03 20:26:05

设置一个布尔值,比如waitForKey=true,然后在像Update这样的内容中:

代码语言:javascript
复制
if( waitForKey && Input.GetKeyDown("space") ) {
    waitForKey=false;
    StartNextWave();
}
票数 0
EN

Stack Overflow用户

发布于 2022-04-03 20:31:20

侦听Update函数中的按钮输入。

代码语言:javascript
复制
void Update() 
{
    if (_enemiesInWaveLeft <= 0 && InputGetKeyDown(KeyCode.Space))
    {
        StartNewWave();
    }
}

这将导致波产生时,没有剩下的敌人和Space是按下。您需要单独添加一些UI,以便给玩家提供一些反馈,说明何时可以按下Space按钮。

还可以从您的StartNextWave函数中删除EnemyDefeated。这可以用于启用和禁用用户界面,让播放器知道按下一个按钮继续。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71729534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档