Random but with care and balance

Andrea Alicino
2 min readApr 22, 2021

--

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.

--

--

Andrea Alicino
Andrea Alicino

Written by Andrea Alicino

Game Developer Unreal Engine/Unity. Computer science graduate. Seeking new opportunities.

No responses yet