It’s been awhile since I worked on my game and I am stuck. I have my spawn script that has an array of enemy prefabs. I want it so the next spawned enemy will be different than the last. I don’t want back to back spawns of the same enemy.
Any help on how to do this? Here is a snippet of the script I currently have:
int lastSpawn := last spawned id
int enemyTypeCount := count of enemy types
int id = Random.Range(0, enemyTypeCount-1)
if (id == lastSpawn)
index++
lastSpawn = id
This makes sure, that every enemy has the same spawnchance, excpet for the one spawned last
There’s only one problem, that whatever value you set the lastSpawn, that monster can’t be spawned first
(You could solve this by setting it to -1 then in the Random.Range:
0, enemyTypeCount - (lastSpawn == -1 ? 0 : 1)
Note that using random.range as an int does not require you to subtract 1. If you subtract 1, you’ll actually not spawn all groups as the int version is not inclusive on the last number.
I messed up a bit btw, it should be if (id >= lastSpawn)
So you didn’t understand, huh?
it’s meant to leave a gap like so:
possible values 0-5
I generate values from 0-4
if the value is greater than lastspawn, I add one to it, so I get the following possible results: (lastSpawn=3)
0, 1, 2, 3+1, 4+1