Hello everyone!
I am trying to make a drop chance system when killing enemies in my game.
I am doing this:
if (Random.value <= itemChance.probability) {
//drop that item! :smile:
}
That works! However, will this also work if I want something way less than 1% drop?
So I change the probability to something like .0001 or something crazy small?
Or will it never actually hit?
Try this:
void Start() {
StartCoroutine(RandomChance());
}
long runs = 0;
int matches = 0;
float nextprint;
IEnumerator RandomChance()
{
WaitForSeconds wfs = new WaitForSeconds(.4f);
while (true)
{
for(int i = 0; i < 200; ++i)
{
float f = Random.value;
if (f <= 0.0001f) matches++;
runs++;
}
if (Time.time >= nextprint)
{
print("Runs = " + runs + " matches = " + matches);
nextprint = Time.time + 2;
yield return wfs;
}
else yield return null;
}
}
1 Like
No problem.
For any future readers, who may not want to actually run that code⦠yes, it does work
heh.