0
\$\begingroup\$

I'm using the official Unity tutorial for the Space Shooter game, and I'm at the part where the ability to spawn a set amount of hazards in a wave at a certain delay is introduced.

I want to make it so that at the end of a certain level, the speed of the hazards, and the amount of hazards both increase. I'm pretty sure that can be handled in the game controller, but I don't know the commands I would be needing to use to make that happen. This is the existing code:

public class GameController : MonoBehaviour 
{
    public GameObject hazard;
    public Vector3 spawnValues;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;

    void Start ()
    {
        StartCoroutine (SpawnWaves());
    }    

    IEnumerator SpawnWaves ()   
    {
        yield return new WaitForSeconds (startWait);
        while (true) 
        {
            for (int i = 0; i < hazardCount; i++) 
            {
                Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate (hazard, spawnPosition, spawnRotation);
                yield return new WaitForSeconds (spawnWait); 
            }
            yield return new WaitForSeconds(waveWait);
        }
    }
}

What commands would I need to use to increase the amount of hazards that spawn at the end of each round? And likewise, how could I change the Hazard's script so that it will increase in speed by a certain factor at the end of each round? Is there a way I can reference the hazard's script in this script, by any chance?

\$\endgroup\$
  • \$\begingroup\$ What have you tried? For example, increasing the value of hazardCount and decreasing the value of waveWait would be a pretty good start. \$\endgroup\$ – MichaelHouse Jun 17 '15 at 19:55
1
\$\begingroup\$

hazardCount is the number of hazards spawned each round.
spawnWait is the delay between spawning of each Hazard in one Wave.
waveWait is the length of each Wave.

You can add your own variable, WaveCounter that you can increase each Wave. And another variable to control how much you want the spawnWait to decrease each new Wave.

int WaveCounter = 0;
int HazardCountIncrease = 1;
float SpawnSpeedIncrease = 0.1f;

IEnumerator SpawnWaves ()   
{
    yield return new WaitForSeconds (startWait);
    while (true) 
    {
        hazardCount += (WaveCounter * HazardCountIncrease);
        for (int i = 0; i < hazardCount; i++) 
        {
            Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
            Quaternion spawnRotation = Quaternion.identity;
            Instantiate (hazard, spawnPosition, spawnRotation);
            yield return new WaitForSeconds (spawnWait - (WaveCounter * SpawnSpeedIncrease)); 
        }
        yield return new WaitForSeconds(waveWait);
        WaveCounter++;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Another option is something like if(WaveCounter%3==0) hazardCount++;. Where the hazard count would increase every three levels. \$\endgroup\$ – MichaelHouse Jun 17 '15 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.