Enemy Spawner
Im working on a game and i just cant figure out how to fix my enemy spawner. Im aiming for a certain behavior which is spawning easy enemies x20( or any no.) then medium enemies and after that a endless spawn of random easy, medium and hard enemies . I also want between each spawn to have like a delay or break for 1-2 seconds so it doesn't looks like the enemies are spamming on top of each other
Currently i'm having only easy and medium enemies to spawn and its ending its not endless besides im getting an array out of range error
Your help is appreciated and thank you in advance This is my Code:
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
public GameObject[] enemy;
public Transform[] spawnPoints;
private float timer = 2;
int index = 0 ;
int wave = 0;
List <GameObject> EnemiesList = new List<GameObject>();
private int enemyCount=20;
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0 && wave < 6)
{
timer = 3;
if (wave != 0 && wave % 2 == 0)
{
index ++ ;
}
EnemySpawner();
wave++;
}
}
void Spawn ()
{
for (int i = 0; i<enemyCount;i++)
{
Invoke("EnemySpawner" , i + 2);
}
}
void EnemySpawner ()
{
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
GameObject InstanceEnemies= Instantiate ( enemy[index] , spawnPoints[spawnPointIndex].position , spawnPoints[spawnPointIndex].rotation) as GameObject;
EnemiesList.Add(InstanceEnemies);
}
Answer by Mindmapfreak · Jun 25, 2016 at 10:53 AM
I am not sure what wave and index exactly do in your code. I thought there are only 3 waves (only easy enemies, only medium enemies, random enemy types) so why are you check if wave is less than 6? I would do it like this:
public class Spawner : MonoBehaviour {
public GameObject EasyEnemey;
public GameObject MediumEnemey;
public GameObject HardEnemey;
public Transform[] SpawnPoints;
public float TimeBetweenSpawns;
public int NumberOfEasyEnemiesToSpawn;
public int NumberOfMediumEnemiesToSpawn;
public float EasyChance;
public float MediumChance;
public float HardChance;
private int waveNumber;
private float spawnTimer;
private int numberOfEasyEnemies;
private int numberOfMediumEnemies;
// Use this for initialization
void Start()
{
this.spawnTimer = 0.0f;
this.waveNumber = 0;
float totalChance = this.EasyChance + this.MediumChance + this.HardChance;
if(Mathf.Abs(totalChance-1.0f)>0.0001f) {
Debug.LogWarning("Warning: The chances should add up to 1.0 ("+totalChance+" currently)");
}
}
// Update is called once per frame
void Update()
{
this.spawnTimer -= Time.deltaTime;
if(this.spawnTimer<=0.0f)
{
Transform spawnPoint = this.SpawnPoints[Random.Range(0, this.SpawnPoints.Length)];
Vector3 spawnPos = spawnPoint.position;
Quaternion spawnRot = spawnPoint.rotation;
switch(this.waveNumber)
{
case 0:
Instantiate(EasyEnemey, spawnPos,spawnRot);
this.numberOfEasyEnemies++;
if(this.numberOfEasyEnemies>=this.NumberOfEasyEnemiesToSpawn)
{
this.waveNumber++;
}
break;
case 1:
Instantiate(MediumEnemey, spawnPos, spawnRot);
this.numberOfMediumEnemies++;
if (this.numberOfMediumEnemies >= this.NumberOfMediumEnemiesToSpawn)
{
this.waveNumber++;
}
break;
case 2:
float randomFloat = Random.value;
if(randomFloat<this.EasyChance)
{
Instantiate(EasyEnemey, spawnPos, spawnRot);
}
else if(randomFloat<this.EasyChance+this.MediumChance)
{
Instantiate(MediumEnemey, spawnPos, spawnRot);
}
else
{
Instantiate(HardEnemey, spawnPos, spawnRot);
}
break;
}
this.spawnTimer = this.TimeBetweenSpawns;
}
}
}
Hello, Can you explain the totalchance logic? i really dont get it.
Thank you.
If you want to spawn 50% easy enemies, 30% medium enemies and 20% hard enemies in the last wave, you have to set EasyChance to 0.5, $$anonymous$$ediumChance to 0.3 and HardChance to 0.2. The chances should naturally add up to 1.0. Random.value gives you a float between 0 and 1. The if-else-conditions in this example basically are (pseudocode)
if(randomFloat between 0 and 0.5) {//0.5 out of 1.0 = 50%
spawnEasyEnemy();
}
else if(randomFloat between 0.5 and 0.8) {//0.3 out of 1.0 = 30%
spawn$$anonymous$$ediumEnemy();
}
else if(randomFloat between 0.8 and 1) {//0.2 out of 1.0 = 20%
spawnHardEnemy();
}
EDIT: Or do you mean the condition in Start()? That one just checks if totalChance differs from 1.0 and factors in floating point precision.
i had in the inspector to spawn 5 easy and 5 medium it spawns 5 easy and 5 medium then it keeps spawning easy forever. it's never spawning medium or hard again.
What values did you set EasyChance, $$anonymous$$ediumChance and HardChance to?
Thank you very much. this is working perfectly. And i understand everything now.
Thanks Again :)