- Home /
Sound-Based Enemy Spawning
Hello!
I'm looking to create an enemy spawner that spawns an enemy every time the song has a bass note/kick etc. Any ideas? Current spawn script is:
public Transform spawnSmallEnemy; //Enemy to spawn
public Transform spawnLargeEnemy; //Enemy to spawn
public Transform spawnPosition; //Where we spawn from
public int timeTillSpawn = 0; //Increases to count when we need to spawn next
public int spawned = 0; //How many enemies have been spawned so far
public int enemyspawnrate = 300; //The current spawn rate
public int oldspawnrate = 300; // Let's us store the old spawn rate so we can check if it's changed and stop changing the current rate
public int newspawnlimit = 20;
void Update()
{
if (Time.timeScale == 1)
{
if (timeTillSpawn >= enemyspawnrate)
{
Instantiate(spawnSmallEnemy,Random.insideUnitSphere * 10 + transform.position,spawnSmallEnemy.transform.rotation);
timeTillSpawn = 0;
spawned+= 1;
oldspawnrate = enemyspawnrate;
}
else
{
timeTillSpawn += 1;
}
if (spawned == newspawnlimit && oldspawnrate == enemyspawnrate)
{
enemyspawnrate = enemyspawnrate / 2;
newspawnlimit = newspawnlimit * 2;
Instantiate(spawnLargeEnemy,Random.insideUnitSphere * 10 + transform.position,spawnLargeEnemy.transform.rotation);
}
}
}
Answer by jpthek9 · Apr 06, 2015 at 05:26 PM
You'll need some sort of sound recognition plugin. I don't know of any on the asset store. Maybe write it yourself?
If that's too much work, just hardcode the mechanic in. You can use a coroutine to time the spawns. Note that if framerate drops too low, WaitForSeconds() might not be very accurate and the spawner could get out of sync from the music. It'll help to have the sheet music for this:
void Start()
{
Audio.Play();
StartCoroutine(Spawner());
}
IEnumerator Spawner()
{
yield return new WaitForSeconds (5)
Instantiate(Creep);
yield return new WaitForSeconds (1)
Instantiate(Creep);
yield return new WaitForSeconds (1)
Instantiate(Creep);
yield return new WaitForSeconds (1)
Instantiate(Creep);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613192244im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
problem Insatiate and destroy auto 0 Answers
Instatiate a Prefab, assign a public variable - get NullReferenceException 1 Answer
Accesing script from newly instantiated game object 1 Answer
Instantiating a variety of meshes 1 Answer
Procedural Generation - Rooms spawning at same positions. 0 Answers