Home > Net >  How would i pause before continuing waves in unity?
How would i pause before continuing waves in unity?

Time:04-04

im making an enemy wave - based game and it works fine, but i want to make it so before it continues on to the next wave, the player has to press a key to let the game know they're ready, the reason i want to do this is because i want to implement a shop to purchase upgrades after waves, here is my wave script:

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();
        }
    }
}

CodePudding user response:

Set a Boolean like waitForKey=true, then in something like Update:

if( waitForKey && Input.GetKeyDown("space") ) {
    waitForKey=false;
    StartNextWave();
}

CodePudding user response:

Listen to a button input in the Update function.

void Update() 
{
    if (_enemiesInWaveLeft <= 0 && InputGetKeyDown(KeyCode.Space))
    {
        StartNewWave();
    }
}

This will cause the wave to spawn when there are no enemies left and Space is pressed down. You will need to add some UI separately to this to give the player some feedback as to when it is Ok to press the Space Button.

Also remove the StartNextWave from your EnemyDefeated funciton. That can be used to enable and disable UI for the player to let them know to press a button to continue.

  • Related