- Home /
How to make pickups Rare(Space Shooter Game)
Okay so my problem is, I'm having trouble making Space Shooter Pick up rare. There's an array full of different hazards. One of those hazards are rare so I don't want it to spawn constantly like the other ones. My code seems fine to me but it doesn't work the way I want it to..
for (int i = 0; i < hazardCount; i++)
{
//Problem with making Pickups Rare...
int rand = Random.Range (0, hazards.Length);
Debug.Log (rand);
GameObject hazard = hazards [rand];
if (rand == 5){
int genRand = Random.Range (0, hazards.Length);
//If rand is 5 then generate another number
if (rand == 5 && genRand == 5){
//If both of those numbers are the same then spawn the rare pickup.
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}else{
//If not then spawn the hazard under it.
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazards[rand - 1], spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
}else {
//If the number is not 5 then skip all code above and spawn the regular hazard.
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
}
Yes it spawns it just like any other hazard.. I want the rare ones to come like every once in a while..
Answer by Guppie1337 · May 11, 2015 at 09:23 PM
I've recently had a lot of fun doing this same thing. The few ways that I felt worked for me are as follows:
1) Place the rare item(s) at the end of the array. If you are choosing random items from that array you could cut off the end with Array.length - (however many rare items exist at the end). Whenever you spawn something through your loop, you can have your own counter determine when to spawn your rare item(s) and redefine the Instatiated item to match the index of the rare item you wish to spawn.
public GameObject[] spawnItems;
public int spawnCounter;
private int spawnIndex;
NameOfFunction ()
{
while (true)
{
spawnIndex = Random.Range (0, (spawnItems.length - (//number at the end));
//Assuming you want rare to be 1%.
if (spawnCounter > 99)
{
//Redefining the spawnIndex to be the rare item.
spawnIndex = spawnItems[//rare item index goes here];
//Reset spawnCounter
spawnCounter = 0;
}
Instantiate (spawnItems[spawnIndex], spawnPosition, Quaternion.whatever);
spawnCounter += 1;
}
}
2) The other way I have done it is to make the rare items their own single GameObject or place all rare items in their own array and spawn between those the same way as shown above.
3) The last way I did this was by using a modulo operator. This can be useful when spawning items by intervals. If that interests you, I'd look them up. Hope I helped.
Just another re$$anonymous$$der though. I edited my answer to exclude the Update () function. In my experience, if you're using C#, you should put spawning in a loop inside of a Coroutine. They are very simple to set up and it allows you to yield values such as wait times/delays to spawn.
If you haven't figured out your last issue, you can assign spawnCounter a value of 0 at start, awake, or even with your declarations.
Answer by FortisVenaliter · May 12, 2015 at 09:22 AM
It's because you are caching the variable hazard.
After you initialize the variable genRand, you need to add a new line:
hazard = hazards [genRand];
That way the hazard is chosen with the new index.
Answer by zviaz · May 11, 2015 at 07:45 PM
You could use Random.value to spawn items based on a percentage.
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
}
All 3 conditions will return true if val > 0.7 . You need to get the Random.value first, and check if ... else ...
float rndVal = Random.value;
if ( rndVal < 0.05f )
{
// 5%
}
else if ( rndVal < 0.2f )
{
// 15%
}
else if ( rndVal < 0.5f )
{
// 30%
}
else
{
// 50%
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Multiple Weapon System Implementation. 1 Answer
Picked Up object - how to create correct collision detection 0 Answers