When you begin development on a system for your game that involves the spawning of loot, powerups, enemies, or whatever, it is important to take “Balance” into account. Too many enemies of one type and not another can imbalance your combat or make it feel basic and unimpressive. Equal chances in loot distribution can counteract an attempt at creating rarity/more valuable items.
In order to make a system that fits your specific needs, you will need to add parameters that dictate higher chances for some things to happen and lower chances for others. This can be done by using random ranges of integers to create a percentile-based trigger! Let’s see how that’s done!
This script is my “SpawnManager” script and oversees the spawning and distribution of my powerups.
This is my “Powerup” script and contains the parameters that define/identify my individual powerups.
The actual methods that contain the functions of each powerup are found on my “Player” script.
When creating a Random.Range value, it is important to remember that when using an integer (int) value the min number will be inclusive while the max number will be exclusive. That means that if you have a range of (1, 100) it will produce a min value of 1 and a max value of 99. If you wanted to produce a range of numbers from 1 to 100 you would have to do Random.Range(1, 101)
But while the max value for an integer’s range is exclusive, the max value for a float’s range is not. If using a range of float numbers such as with Random.Range(1.0f, 100.0f), both the min AND the max values are inclusive.