Hello fellow community members,
This might be a bit tricky to phrase…
So I’m working on a spawning system that continuously spawns enemies, 1 every second. While that’s easy, different types of enemies have different chances to spawn, and they don’t all start to spawn from the beginning. I’m a bit stuck on how I should do the random picker, based on percentage, while all chances combined don’t form 100%. Though it still always should spawn an enemy.
So let’s give an example
At first it only is able to spawn one type of enemy. I’ll call this the defaultEnemy. So this prefab starts with 100% chance to spawn basically, although this is not a set chance.
Once enough time has passed, more enemies are allowed to spawn. Let’s say Enemy1 has a 10% chance, Enemy2 has 8%. (The numbers are defined by a variable in their script/scriptableObject.) That means when those two are allowed to spawn, it has 10% chance to pick Enemy1, 8% chance to pick Enemy 2. If it picks neither, it should spawn the defaultEnemy. So that one now has 82% chance to spawn.
And so more enemies become available to spawn over time. So the chance it picks nothing (the defaultEnemy) varies, as it decreases every time another type becomes available to spawn.
So at the end it could look like this:
Enemy1: 10% (time > 10)
Enemy2: 8% (time >12)
Enemy3: 12% (time > 60)
Enemy4: 6% (time > 70)
Enemy5: 4% (time > 120)
Chance to pick nothing (= defaultEnemy) after time has passed 120 : 60%
One goal I want with the script is that I can easily add more enemies, without making changes to the script, just by adding more in the array/list that it can spawn in the Unity inspector. That may be the hardest part even. So it would have to do a loop for each enemy in the array… but still resolve the pick chance of all of them at once…
Your advise would greatly appreciated.
Edit: using C#