• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bigmannyb · May 11, 2015 at 12:19 PM · c#spaceshooterpickup

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);
                 }
             }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · May 11, 2015 at 04:17 PM 0
Share

It's spawning them evenly still?

avatar image Bigmannyb · May 11, 2015 at 07:32 PM 0
Share

Yes it spawns it just like any other hazard.. I want the rare ones to come like every once in a while..

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bigmannyb · May 11, 2015 at 09:46 PM 0
Share

What do I set spawnCounter as?

avatar image Bigmannyb · May 11, 2015 at 10:31 PM 0
Share

Thanks I got everything all set! You're a life saver.

avatar image Guppie1337 · May 12, 2015 at 07:27 AM 1
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
  }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bigmannyb · May 11, 2015 at 08:25 PM 0
Share

Alright I'll give it a try.

avatar image AlucardJay · May 11, 2015 at 08:30 PM 1
Share

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%
 }

avatar image zviaz · May 11, 2015 at 08:34 PM 0
Share

Good point not sure how I missed that.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Multiple Weapon System Implementation. 1 Answer

Item pickup and drop function 2 Answers

Weapon pick up 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges