Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 coolbird22 · May 11, 2014 at 04:42 PM · randomenemyspawn

How to reduce the chances of a particular enemy spawning ?

I have an array of enemy prefabs inside which, I'm adding the different kinds of enemies I want it to spawn. Currently it spawns any enemy it wants to and there is no control over how many times I want a particular enemy to spawn. How do I make it so that a particular enemy spawns rarely ? Here is my current code:

     public float spawnTime = 5f;        // The amount of time between each spawn.
     public float spawnDelay = 3f;        // The amount of time before spawning starts.
     public GameObject[] enemies;        // Array of enemy prefabs.
     
     void Start ()
     {
         // Start calling the Spawn function repeatedly after a delay .
         InvokeRepeating("Spawn", spawnDelay, spawnTime);
     }
     
     void Spawn ()
     {
         // Instantiate a random enemy.
         int enemyIndex = Random.Range(0, enemies.Length);
         Instantiate(enemies[enemyIndex], transform.position, transform.rotation);
     }
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 coolbird22 · May 11, 2014 at 05:15 PM 0
Share

What you suggested is an idea I had, I'm not sure how to weave this chance into my current script. How do I make a condition for a particular enemy index inside that array ?

avatar image ANGRYSH4RK · May 11, 2014 at 05:19 PM 0
Share

well before instantiating, check if the enemy is a rare one. then use chance to decide whether to instantiate or return;

3 Replies

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

Answer by coolbird22 · May 11, 2014 at 08:48 PM

Managed to get some help off Reddit. Here is a modified version of my previous code to have a chance based spawn of a particular enemy.

 public float spawnTime = 5f; // The amount of time between each spawn.
 public float spawnDelay = 3f; // The amount of time before spawning starts.
 public float chanceSpawnRare = 0.1f; // Chance that a rare enemy will spawn.
 public GameObject[] normalEnemies; // Array of regular enemy prefabs.
 public GameObject[] rareEnemies; // Array of the rare enemy prefabs.
 
 void Start ()
 {
     // Start calling the Spawn function repeatedly after a delay.
     InvokeRepeating("Spawn", spawnDelay, spawnTime);
 }
 
 GameObject[] spawnArray;
 int enemyIndex;
 
 void Spawn ()
 {
     // Chance to spawn rare, or normal enemies.
     if(Random.Range(0f, 1f) > chanceSpawnRare)
     {
         spawnArray = normalEnemies;
     }
     else
     {
         spawnArray = rareEnemies;
     }
 
     // Instantiate an enemy.
     enemyIndex = Random.Range(0, spawnArray.Length);
     Instantiate(spawnArray[enemyIndex], transform.position, transform.rotation);
 }
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
1

Answer by ANGRYSH4RK · May 11, 2014 at 05:08 PM

You could base it off chance. The example below has a 50/50 chance of spawning.

 float rand = Random.Range(0, 101);
     
 if (rand >= 50) {
     // spawn enemy //
 }
 

for more rarity you could make the enemy spawn with a 5% chance:

 float rand = Random.Range(0, 101);
     
 if (rand >= 95) {
     // spawn enemy //
 }
 

hope it helps :P

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 musaranya · May 11, 2014 at 05:16 PM

One possible approach is to add a variable to the class enemy for counting the times that an enemy is selected by the random before spawn it. For example:

 CommonEnemy.counterToNextSpawn=1;
 RareEnemy.counterToNextSpawn=10;

 int enemyIndex = Random.Range(0, enemies.Length);
 enemy selectedEnemy = enemies[enemyIndex] as enemy;
 selectedEnemy.counterToNextSpawn-=1;
     
 if(selectedEnemy.counterToNextSpawn==0)
 {
    // Initializes the couter to its original value (it depends of the type of the enemy)
    selectedEnemy.counterToNextSpawn=1;  
    Instantiate(enemies[enemyIndex], transform.position, transform.rotation);
 }

This way an enemy only will be spawned when its counterToNextSpawn is equal to 0: common enemies will spawn each time the random points them, rare enemies will need 10 hits of the random function to spawn. You can make a gradient with the counter values or whatever you need, this is just an approach :)

Comment
Add comment · Show 1 · 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 coolbird22 · May 11, 2014 at 07:15 PM 0
Share

Does all of this go inside the Spawn method ? If so, I'm getting an error for the 5th line of the code that says: Error CS0246: The type or namespace name `enemy' could not be found. Are you missing a using directive or an assembly reference?

Also, I'd humbly admit that what you have suggested is a bit advanced for me to understand since I started coding a month ago. Thanks a lot for your help !

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.

Follow this Question

Answers Answers and Comments

21 People are following this question.

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

Related Questions

Make enemies spawn faster with timer? (C#) 2 Answers

Spawning random enemies in spawn points 1 Answer

Endless/infinite runner random enemy spawning multi lane 1 Answer

Spawning enemies randomly without collisions 3 Answers

Problem with random spawning and coroutine 1 Answer


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