- Home /
Question by
Dzejkob1218 · Dec 31, 2015 at 09:39 AM ·
c#randomization
Return one of a few outcomes with different probabilities?
Hi everyone, I want to write a piece of code that spawns enemies in my game. I want it to have a few possible outcomes, each spawning a different kind of enemy. The problem is I want the chances of some of the enemies appearing to be greater than others so Random.Range won't do. I also need to be able to modify the probability of certain types of enemies spawning at runtime to control the difficulty.
I'm still kind of new to Unity and I have no idea how I could do something like this.
Comment
Best Answer
Answer by tanoshimi · Dec 31, 2015 at 11:31 AM
You still use Random.Range...
int randNumber = Random.Range(0,100);
if(randNumber<30){
// 30% chance of this occurring
}
else if(randNumber<90){
// 60% chance of this occurring
}
else if(randNumber>=90){
// 10% chance of this occurring
}
Your answer