How To: spawn succesive waves of enemies increasing their speed

Hi guys!

I am experimenting with one tutorial games provided by Unity: Space Shooter http://unity3d.com/learn/tutorials/projects/space-shooter; And I want to make minor changes to it so I can learn how to further develop this project by adding extra features and so on.

The question is, how could I modify the script, so when a player reaches 1000 points for example to spawn more enemyes, to increase the speed of them and things like that.

using UnityEngine;
using System.Collections;

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

Thanks in advance!

I’ll do some assumptions:

  1. You have a PlayerType class that has a public variable that represents points.
  2. You have a Player game object in your scene which holds the PlayerType script among its components.
  3. You dragged your Player instance into the public field called myPlayer of your GameController component.

The following approach does not work if you have an infinite game.

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{
	
	public PlayerType myPlayer; // Get your player's reference here
	
	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);
				
				// Easy way: Make your list of conditional spawning here
				if(myPlayer.points < 999)
				{
					yield return new WaitForSeconds (spawnWait); // normal spawnWait
				}
				else if(myPlayer.points > 1000 && myPlayer.points < 1999)
				{
					yield return new WaitForSeconds (spawnWait * 0.8f); // Quicker spawnWait
				}
				else if(myPlayer.points > 2000 && myPlayer.points < 4999)
				{
					yield return new WaitForSeconds (spawnWait * 0.7f); // Even Quicker spawnWait
				}
				// go on with how many conditions you wish
				else // last else is just for defensive programming, meaning it takes the cases that should not happen, like player points being < 0
				{
					yield return new WaitForSeconds (spawnWait);
				}
					
				yield return new WaitForSeconds (waveWait);
				}
			}
		}

The other way, if you have an infinite game where difficulty constantly raises depending on player points, you just a need a simple formula like this to make it grow linearly with the maximum difficulty when the player reaches 10000 points and a waiting time of at least 3f (when starting) seconds and minimum (most difficult) 0.1f seconds:

spawnWait = max(0.1f, 3f - ( ((float)myPlayer.points)/10000f ) * 3f );

The spawnWait function is similar to what you see here
but in the code it will be limited between 3 (assuming your player points will always be > 0) and 0.1.

You can also affect the speed of your hazards with the same approach.

Hope this helps.