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 MathewJProductions · Jan 29, 2019 at 11:05 PM · delayfloats

Gradually decreasing float over time

Hello, I'm creating an enemy spawner system for my 2d platformer but can't get the spawn delay to gradually decrease over time. I've looked online but none of the answers seems to work. Here's the code:

  public float delay;
 
     public Transform[] spawnPoints;
     public GameObject[] enemies;
     int randomSpawnPoint, randomEnemy;
     public bool spawnAllowed;
 
     void Start () {
         spawnAllowed = true;
         InvokeRepeating("SpawnAnEnemy", 0f, delay);
     }
 
     void SpawnAnEnemy()
     {
         if (spawnAllowed)
         {
             randomSpawnPoint = Random.Range(0, spawnPoints.Length);
             randomEnemy = Random.Range(0, enemies.Length);
             Instantiate(enemies[randomEnemy], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
 
         }
     }

the delay is around 5 and I want it to slowly decrease to 1 after a certain amount of time. I tried using delay -= -0.001f; in an Update function but it didn't actually change the rate that the enemies were spawning

Any help would be very appreciated, thank you for your time.

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

1 Reply

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

Answer by xxmariofer · Jan 29, 2019 at 11:15 PM

Hello, have you test using coroutines? i find it more flexible and easier to use.

     void Awake()
     {
         StartCoroutine(SpawnAnEnemy());
     }

     IEnumerator SpawnAnEnemy()
     {
         while (spawnAllowed)
         {
                 randomSpawnPoint = Random.Range(0, spawnPoints.Length);
                 randomEnemy = Random.Range(0, enemies.Length);
                 Instantiate(enemies[randomEnemy], spawnPoints[randomSpawnPoint].position, Quaternion.identity);

                 yield return new WaitForSeconds(delay);

                 delay -= delayReduction;//0.5 for example be carefull not make it less than 0

                 if(delay  <= 0) spawnAllowed = false;              
         }
     }
Comment
Add comment · Show 13 · 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 MathewJProductions · Jan 29, 2019 at 11:22 PM 0
Share

This fixed the problem! The coroutine was definitely easier than what I was trying so ill try and use more of them in the future if I can. Thanks for your help :)

avatar image MathewJProductions · Jan 29, 2019 at 11:26 PM 0
Share

By any chance would you know how to put a limit on this? so the delay doesn't go below 1 second?

avatar image xxmariofer MathewJProductions · Jan 29, 2019 at 11:37 PM 0
Share

just an if before the delay reduction and when it gets to 0 change spawnAllowed to false, that will make the while to finish.

avatar image MathewJProductions xxmariofer · Jan 29, 2019 at 11:51 PM 0
Share

so something like

if( delay == 0) { spawnAllowed = false; }

??? sorry I'm not the best at coding

Show more comments
avatar image Bunny83 · Jan 29, 2019 at 11:34 PM 0
Share

Note: in your current setup whenever you set "spawnAllowed" to false your application (and editor) will hang since you're trapped inside an infinite loop without yield. When you create an infinite loop in a coroutine (which is perfectly fine) you have to ensure that you always have a yield statement that is executing.


Sometimes it makes more sense to write it like this:

 while (true)
 {
     while (!spawnAllowed)
         yield return null;
     
     randomSpawnPoint = Random.Range(0, spawnPoints.Length);
     randomEnemy = Random.Range(0, enemies.Length);
     Instantiate(enemies[randomEnemy], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
     yield return new WaitForSeconds(delay);
     delay -= delayReduction;//0.5 for example be carefull not make it less than 0
 }

That way you have a better overview where and when your yields fire.

avatar image xxmariofer Bunny83 · Jan 29, 2019 at 11:39 PM 0
Share

Thats true i didnt take that into account, i will edit the answer thanks :)

avatar image xxmariofer Bunny83 · Jan 29, 2019 at 11:43 PM 0
Share

@$$anonymous$$iniGameDev2208 Take @Bunny83 code into account or the new edited answer, since the previous one would give you problems.

avatar image MathewJProductions Bunny83 · Jan 29, 2019 at 11:54 PM 0
Share

What does the yield return null do? Does it stop the rest of the coroutine if the spawnAllowed is false?

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

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

Related Questions

Make enemy wait before attacking player 2 Answers

AudioSource playing with delay for 1 step 0 Answers

How do I put a delay in this? 2 Answers

Touch input reporting 0 delta for first several Updates on iOS, 2 Answers

Invalid floats in PlayerPrefs 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