I am trying to make spawnpoints where the zombies spawn and keep spawning like a wave system I looked up a basic way to spawn enemies and I’m running into some issues.
-
Right now I am only able to spawn zombies once and when they are dead no more spawn
-
The way the code is set up it makes its very hard to implement for eg. I have to mark the range of x, y and z positions soo zombies can only spawn in that particular area.
-
As my level has elevations because of how I have set up my terrain if the Y values are not set perfectly the zombies spawn in and either have feet buried in the ground or floating in the air
Is there any more efficient way to make this system which works more dynamically and predictably?
EnemySpawner C#
public class EnemySpawner : MonoBehaviour
{
[SerializeField] GameObject enemy;
public int xPos;
public int yPos;
public int zPos;
[SerializeField] float spawnDelay= 01f;
private int enemyCount;
void Start()
{
}
private void Update()
{
StartCoroutine(SpawnEnemy(spawnDelay, enemy));
}
IEnumerator SpawnEnemy(float spawnDelay, GameObject enemy)
{
while (enemyCount < 20)
{
xPos = Random.Range(80, 74);
yPos = Random.Range(50, 51);
zPos = Random.Range(80, 95);
GameObject newenemy = Instantiate(enemy, new Vector3 (xPos, yPos, zPos), Quaternion.identity);
yield return new WaitForSeconds(spawnDelay);
enemyCount += 1;
}
}
}