- Home /
Enemies spawn on top of each other
Hey I will try and explain my situation as best I can. Here is the issue:
I have a script that allows a maximum of 3 enemies to spawn in on start up. This is fine and they spawn at different empty gameobjects located around my map (15 in total, all stored in an array). Sometimes everything is fine and they all spawn at unique points and do not conflict with each other but sometimes, they choose the same spawn point (empty gameobject) as another enemy, thus leaving 2 enemies on top of each other. Here is some more information to help understand my setup:
I have 15 empty gameObjects located around my map. When the game starts, the 3 enemies instantiate at different locations ( random choice of the 15 on the map). Sometimes it works fine but other times they spawn on top of each other making it a possibility for you to hit 2 targets at once which is not what I want. Here is the code:
var car:Transform[];
var spawn:Transform[];
function Start(){
for(var k =0 ;k<3;k++){
var car:Transform = car[Random.Range(0,car.length)];
var spawnpos:Transform = spawn[Random.Range(0,spawn.length)];
Instantiate(car,spawnpos.position,spawnpos.rotation);
}
}
/ Up to now: The script spawns 3 enemies at random points but it is possible for them to use the same point. Is there anyway of stopping this from occurring, I've been hacking away at it for a while now but to no avail lol :( Some users on here have given me solid advice and I have learnt a lot but none of it is relevant to what I actually need, so I hope someone can help. If you need any information please feel free to ask.
The fix: (Please read comment section first):
var car:Transform[];
var car2:Transform[];
var spawn:Transform[];
var spawn2:Transform[];
function Start(){
for(var k =0 ;k<1;k++){
var car:Transform = car[Random.Range(0,car.length)];
var spawnpos:Transform = spawn[Random.Range(0,spawn.length)];
Instantiate(car,spawnpos.position,spawnpos.rotation);
}
for(var t =0 ;t<1;t++){
var car2 : Transform = car2[Random.Range(0,car2.length)];
var spawnpos2:Transform = spawn2[Random.Range(0,spawn2.length)];
Instantiate(car,spawnpos2.position,spawnpos2.rotation);
}
}
Answer by Rati · Oct 28, 2012 at 05:46 PM
Perhaps you could first do 3 "randoms", check if theses randoms are all differents from each other. then use theses values when you select the spawn ?
Hmmm, that just gave me an idea, I could in effect produce 3 spawn arrays and spawn 1 enemy in each of the 3 arrays, thus making 3 enemies appear. This would work, right? Although not efficient, it would be a fix, right?
It will reduce the spawn "random diversity", but, it should be a fix, yes
yeah I just tested it then with different values, although I will get shouted at off the Unity police for marking this as an answer, I'm going to do it anyway and explain in the next comment how I went about it. Indirect solution my friend, kudos to you!
The solution for anyone who may want to stop objects spawning on top of each other:
You can use the code provided above which will enable a maximum of 3 enemies to spawn within your level ( the k value indicates the amount of enemies allowed). Basically all you want to do is duplicate this code and give different objects different values for example (Please check my question, I will add the working method to the end). I hope this helps and thank you Rati.
Answer by Dexter.Unity · Oct 28, 2012 at 06:20 PM
Well I think you should save your (array index of the spawn position) -> savedPositions = array();
When it makes a new random value, let it check if it already exists in your savedPositions array.
If not
-> make the monster span
else
--> make a new random value and check it.
This is the method I first thought of actually the problem is the else. How are you going to do a correct loop until it finds one that it can use?
Your answer
![](https://koobas.hobune.stream/wayback/20220613083014im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy Spawning Place 0 Answers
Make enemies spawn faster with timer? (C#) 2 Answers
Enemy not spawning correctly 1 Answer