- Home /
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!
Increase the hazardCount variable when play score reaches a threshold...
So reference the script and in the scorecounter class increase the hazardCount in the update after checking .
It being a coroutine SHOULDN'T be an issue.
Hazard count is the amount of enemies that are spanwed. I picture in another script there must be a points value, which you can make it static (if it is not already). After you made it static you can access it in your spawner script just by ClassName.points and do an if clause , in a update function that you will create, to increase hazard count depending on it. To increase spawning speed, simply make spawnWait smaller.
To increase the speed of the enemies you will do the same things in the enemy behaviour script.
Answer by Rs · Feb 24, 2015 at 12:14 PM
I'll do some assumptions:
You have a PlayerType class that has a public variable that represents points.
You have a Player game object in your scene which holds the PlayerType script among its components.
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.
Your answer
Follow this Question
Related Questions
Spawing Enemies works but works to good 2 Answers
Enemy spawn issues 1 Answer
How to store enemies spawn schedule for each scene ? 1 Answer
Increase number of enemies spawned per level 0 Answers
Changing the spawn rate 0 Answers