- Home /
How to spawn prefabs with percent random?
I need to instantiate 3 different prefabs.
First prefab must spawn with 50 %
Second prefab must spawn with 30 %
Third prefab must spawn with 20 %
I know how to intantite objects, but i didnt know how to make percent random.
Answer by jogo13 · Jan 22, 2013 at 06:10 PM
Random.value returns a float between 0 to 1. You can use it to generate percentage random like so:
if(Random.value > 0.5) //%50 percent chance
{//code here
}
if(Random.value > 0.2) //%80 percent chance (1 - 0.2 is 0.8)
{ //code here
}
if(Random.value > 0.7) //%30 percent chance (1 - 0.7 is 0.3)
{ //code here
}
what if it returns 0.6, then it would run the first and the second if statement...?
Nope. On each of those if statements a new Random.value is generated. It would work as you've thought only if a variable has been set to be Random.value once, and then re-used in all those if statements.
Sorry for the necroposting here but isn't there a chance that it doesn't instantiate with that code?
I'm using this code for a chance of either having a health potion be dropped, or the player taking damage. Sometimes they both happen, or twice of one or another happen. Is there any way to fix this?
Answer by Kalbytron · Sep 09, 2013 at 11:15 PM
You can also say
if(Random.value <= 0.2)
to get a %20 chance without the confusion.
Wouldn't it be if(0.2 > Random.value)?
for example, say the random value generated a 30%
20% is NOT greater than 30%, therefore the if statement returns false.
however, when the random value generates something like a 15%
20 IS greater than 15, therefore the code will run.
That's the same thing as saying if(Random.value < 0.2) @DigitalCyclone; the <= is just to get the .20 along with all of the other values that are below it.
problem with this is random.value is 0 to 1 [INCLUSIVE] which means that it is possible for the value to be 0, if the chance value was set to 0.0 which means it should never happen since your using <= then there is a slight chance that it could fire.
Answer by unity_973D59F5B05CFF0E9822 · Aug 01, 2021 at 07:26 AM
public float Chance()
{
float[] chances = new float[] { 0.5f, 0.3f, 0.2f }; // Your chances. Summary = 1.0f
float randValue = UnityEngine.Random.value; // Randome value (chance value)
float currentChance = 0f;
float minChanceRange;
float maxChanceRange;
for (int i = 0; i < chances.Length; i++)
{
currentChance += chances[i];
minChanceRange = 0.5f - currentChance / 2;
maxChanceRange = 0.5f + currentChance / 2;
if (randValue >= minChanceRange && randValue <= maxChanceRange)
{
return chances[i]; //code here
}
}
return -1; //This code unreacheble
}
Your answer
Follow this Question
Related Questions
How do I make a game object spawn several times in semi-random locations? 1 Answer
I want to swap randomly 5 blocks (block has 5 diffrent color) and spawn in vertical line 1 Answer
Weighted Random Spawning 1 Answer
Enemies spawn on top of each other(C#)(Unity) 1 Answer
How do I randomly create 2 objects, each with a different shape and color? 1 Answer