Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Internetman · May 29, 2016 at 02:29 PM · randomenemyspawntimer

Make enemies spawn faster with timer? (C#)

I have a timer in my 2D survival game that always counts upwards, now I spawn out a new enemy between 4 and 6 seconds with the help of IEnumerator. But I have a little problem, since I want the game to be harder and harder I want to spawn out more enemies and faster then 4 and 6 seconds the further the timer gets.

So say that the "main timer" is set at a random range between 30, 60 seconds. When this number is set I want to spawn out enemies faster, say between a random range at every 1 and 3 seconds. What is the best way to do this? I tried using an If statment but it doesn't work that well. What do you suggest?

Here's the code for spawning the enemies:

 // Spawn a Bouncing enemy
     IEnumerator SpawnBouncingEnemy() 
     {
         //Wait 3 to 5 seconds when game starts to spawn a ball
         yield return new WaitForSeconds(Random.Range(1, 3));
 
         while(true)
         {
             //Calls the function to set random position
             Vector3 spawnPoint = RandomPointWithinBorders();
             
             // Show spawn location for one second
             Object marker = Instantiate(showEnemySpawn, spawnPoint, Quaternion.identity);
             yield return new WaitForSeconds(1);
             Destroy(marker);
             
             // Spawn enemy
             GameObject newEnemy = (GameObject) Instantiate(EnemyBouncingPrefab, spawnPoint, Quaternion.identity);

                     //Set the enemy to active in the List
             activeEnemies.Add(newEnemy);

                     //Wait for 4, 6 seconds before the enemy spawns again!
             yield return new WaitForSeconds(Random.Range(4, 6f));

                     //What is the best way to do this? TimerScript.timer is the "Main timer" in the game
             if(TimerScript.timer >= Random.Range(30, 60))
             {
                 SpawnBouncingEnemy();
                 yield return new WaitForSeconds(Random.Range(1, 1));
                 countNewBouncingEnemies += 1;
                 print(countNewBouncingEnemies);
             }
 
         }
         
     }    


Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by allenallenallen · May 29, 2016 at 02:58 PM

Why couldn't you use Random.Range(30, 60) earlier in the code?

 // Get random time.
 float randTime = Random.Range(30, 60);
 
 // Wait 3 to 5 seconds when game starts to spawn a ball
 yield return new WaitForSeconds(Random.Range(1, 3));

Later, you can just type this:

 if(TimerScript.timer >= randTime) {
 }

This way, the random time won't keep changing each time you spawn a new enemy.

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 Eudaimonium · May 29, 2016 at 03:01 PM

Your timer mechanics appear to be needlesly complicated...

To start out, replace the timing system with a simple variable in any component's Update loop, for example (pseudocode warning):

 public float InitialTime = 8f;
 public float timer = 8f;
 public float timingModifier = 1.1f;
 
 /// somewhere, in an Update loop in a script far away...
 
 timer -= Time.deltaTime;
 
 if (timer < 0)
 {
     timer = (InitialTime / timingModifier);
     timingModifier += timingModifier;
 }

This will make enemies spawn at first 8 seconds apart, then shorter and shorter additively.

Good mathematical solution beats 19 "if" branches :D

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

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

48 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning random enemies in spawn points 1 Answer

Endless/infinite runner random enemy spawning multi lane 1 Answer

How to reduce the chances of a particular enemy spawning ? 3 Answers

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