- Home /
Enemies spawning on top of each other fix?
Hello, I want to fix my script, but I don't know exactly how to do it and I've been googling for awhile now and I haven't found any help. I want 3 types of enemies to respawn, but sooner or later the enemies are spawning on top of each other. What could I use for a solution?
Here's my script: Enemywave.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemywave : MonoBehaviour
{
public Transform enemy1;
public Transform enemy2;
public Transform enemy3;
public float SpawnCount = 3f;
public float WaveCount = 3f;
private int EnemyNum=0;
public Transform whereSpawn;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (WaveCount <= 0)
{
StartCoroutine(EnemyWaveCount());
EnemyWaveCount();
WaveCount = SpawnCount;
}
WaveCount -= Time.deltaTime;
}
IEnumerator EnemyWaveCount()
{
EnemyNum++;
for (int i = 0; i < EnemyNum; i++)
{
Enemy1Spawner();
yield return new WaitForSeconds(1f);
Enemy2Spawner();
yield return new WaitForSeconds(3f);
Enemy3Spawner();
yield return new WaitForSeconds(5f);
}
}
void Enemy1Spawner()
{
Instantiate(enemy1,whereSpawn.position,whereSpawn.rotation);
}
void Enemy2Spawner()
{
Instantiate(enemy2, whereSpawn.position, whereSpawn.rotation);
}
void Enemy3Spawner()
{
Instantiate(enemy3, whereSpawn.position, whereSpawn.rotation);
}
}
What can I do or use in order not to make the enemies to spawn on top of each other? Thank you
Answer by Vega4Life · Nov 12, 2018 at 02:45 PM
First why is this a thing?
StartCoroutine(EnemyWaveCount());
EnemyWaveCount(); // This one won't be called and should be causing a warning
Second, EnemyNum seems to be a big issue. It will eventually, create a ton of dudes, and goes against the point of the EnemyWaveMethod. Just get rid of the entire for loop.
IEnumerator EnemyWaveCount()
{
EnemyNum++; // This never gets decremented and will eventually get really large
Lastly, your enemies share the same spawn point. You could make whereSpawn an array of points around a map, but you would still get spawns on top of each other. I would instead spawn your enemies randomly around the spawn point. Something like:
float distance = 5; // whatever you want the circle size to be
Vector3 spawnPoint = whereSpawn.Position+ Random.insideUnitCircle * distance;
@Vega4Life Thank you for replying. The thing is if I'll create whereSpawn as an array the enemies won't be following along the path and for whatever reason the Navmesh isn't working for me.. Thank you. I'll try it out and let you know.
@Vega4Life StartCoroutine(EnemyWaveCount()); does not cause me any warnings.
float distance = 5; // whatever you want the circle size to be
Vector3 spawnPoint = whereSpawn.Position+ Random.insideUnitCircle * distance;
After implementing your last script it didn't spawn any enemies at all.
So this doesn't work?
float distance = 5; // whatever you want the circle size to be
Vector3 spawnPoint = whereSpawn.Position + Random.insideUnitCircle * distance;
Instantiate(enemy3, spawnPoint , whereSpawn.rotation);
This should definitely work.
Oh, you probably need to keep the Y vector of the original spawn point, or it could spawn under the map I guess. So do this:
float distance = 5; // whatever you want the circle size to be
Vector3 spawnPoint = whereSpawn.Position + Random.insideUnitCircle * distance;
spawnPoint = new Vector3(spawnPoint.x, whereSpawn.position.y, spawnPoint.z); // $$anonymous$$eep the height
Instantiate(enemy3, spawnPoint , whereSpawn.rotation);