Random but with care and balance

when you work with code, it doesn’t matter what language or subject you use it for, but there is always a library that allows you to get a value randomly.

Well also Unity offers well more than one, as an example we want to simulate a dice to 6 faces or to 20 faces

the min value is included the max value is excluded

All this is very nice at the beginning but if not controlled it will lead us to total chaos.

So you have to think of a method that manages the probability, in the case of the dice all the faces have the same weight in terms of probability of coming out. If I want to have a result more frequent than others we have to manipulate it so that the frequency is controlled.

In my case I’m talking about the enemies and power-ups that have been implemented.

part of my enemies spawn coroutine

In this case I assigned a probability directly to the condition of my specific object. This is not recommended because it works when you know the exact position of the objects in its array.

Power-ups spawn coroutine

here instead I always do the same thing but I connect to a variable that belongs to each power-up.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store