- Home /
Wave Spawner | Instaniating Enemys will Overlap
I am creating a wave Spawner with SpawnPositions but sometimes the Enemies will Overlap...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnController : MonoBehaviour {
public Transform[] spawnPos;
public GameObject enemyPrefab;
public float timeBetweenWaves = 1f;
public float timeToSpawn;
public int enemysEachWave;
GameObject clone;
void Start () {
Spawn();
}
void Update () {
if(Time.time >= timeToSpawn)
{
Spawn();
timeToSpawn = Time.time + timeBetweenWaves;
}
}
void Spawn()
{
for (int i = 0; i < enemysEachWave; i++)
{
clone = Instantiate(enemyPrefab, spawnPos[Random.Range(0, spawnPos.Length)].position, Quaternion.identity);
}
}
}
Do you mean they overlap in space or they overlap in time? :)
I mean that 2 Enemies are spawning on top of each other. :)
Answer by Harinezumi · Feb 13, 2018 at 01:04 PM
To avoid spawning enemies at the same position within a wave you need 1) at least as many spawn positions as enemies 2) only use each spawning positions once.
The first needs to be set up by you.
For the second, either shuffle the array of spawning positions, or you create a list of the positions and remove a position every time it is used (no worries, the original array remains. See below the second approach:
using System.Collections.Generic; // add this to the top
void Spawn () {
List<Transform> unusedSpawnPositions = new List<Transform>();
unusedSpawnPositions.AddRange(spawnPos); // add the positions defined to the list
for (int i = 0; i < enemysEachWave; ++i) {
if (unusedSpawnPositions.Count < 1) { break; } // protect against empty list
int spawnPosIndex = Random.Range(0, unusedSpawnPositions.Count); // select index of spawn position
clone = Instantiate(enemyPrefab, unusedSpawnPositions[spawnPosIndex];
unusedSpawnPositions.RemoveAt(spawnPosIndex); // remove spawn position
}
}
Answer by melsy · Feb 13, 2018 at 01:11 PM
So i have thought abou this issue with a project i was working on awhile back and what i was thinking about doing was this. Create a dummy game object in the scene with a collider the same size as the enemy. Reference the empty game object on the wave spawner.
When the wave spawner wants to spawn an enemy , have it move the dummy object to the point and if it hits collision randomize the location again. Then set the dummy to Setactive(false) and do the process again .
pseudo code
dummyObject = DO
wave spawner wants to spawn.
DO is moved into that position
DO is setactive(true)
if DO has collision
wave spawner gets a new position
DO is moved to that new position
if no collision
spawn the enemy.
Interesting approach, but you can't detect collision in the same frame that you are currently, only in the next physics update. A raycast or even capsulecast would be better then, but possibly more expensive.
for what i was doing with simple enemies spawning randomly then walking around a frame didn't matter. I never tried to make it work i just thought about it over time.
Answer by yummy81 · Feb 13, 2018 at 12:56 PM
It seems to me that you did not add any script to your enemyPrefab which would put the instance of this prefab into motion. It can be also that the script does exist but is not enabled. Or some public variables (like speed) are not assigned in the inspector. It's hard to say, but try to attach this simple script to your enemyPrefab. If clones start moving that means you have the solution:
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed = 3f;
private void Update()
{
transform.position += Time.deltaTime * speed * transform.forward;
}
}
Thanks but my Problem I need a solution for is the Spawning because sometimes 2 Enemies spawn on the same Spawnpoint so they overlap. Sorry, my Question wasn't clear enough :/
Your answer
Follow this Question
Related Questions
Photon Gun Pickup Not Showing On All Screens 0 Answers
Create objects on a mouse click in 2D 1 Answer
Random instantiate at same frame with each instantiate having unique random direction 1 Answer
The prefab you want to instantiate is null. 2 Answers
How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer