我正在制作一个基于波浪的游戏,它运行得很好,但是我想让它在它继续到下一波之前,玩家必须按下一个键才能让游戏知道他们已经准备好了,我之所以想这么做是因为我想要实现一家商店来购买海浪后的升级,这是我的wave脚本:
SpawnManager.cs
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();
}
}
}发布于 2022-04-03 20:26:05
设置一个布尔值,比如waitForKey=true,然后在像Update这样的内容中:
if( waitForKey && Input.GetKeyDown("space") ) {
waitForKey=false;
StartNextWave();
}发布于 2022-04-03 20:31:20
侦听Update函数中的按钮输入。
void Update()
{
if (_enemiesInWaveLeft <= 0 && InputGetKeyDown(KeyCode.Space))
{
StartNewWave();
}
}这将导致波产生时,没有剩下的敌人和Space是按下。您需要单独添加一些UI,以便给玩家提供一些反馈,说明何时可以按下Space按钮。
还可以从您的StartNextWave函数中删除EnemyDefeated。这可以用于启用和禁用用户界面,让播放器知道按下一个按钮继续。
https://stackoverflow.com/questions/71729534
复制相似问题