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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by 3 · Mar 17, 2013 at 05:15 AM · instantiateenemyspawnspawningwaves

How would I create a script that spawns objects more frequently as time goes on?

For my game, I have an enemy that works perfectly. Now how would I make a mode, in which the enemy spawns maybe 1 for the first minute, then 20 at the twentieth minute. So that every minute, the spawn goes up 1 or 2. I have no idea where to start with a script like this, so any help would be appreciated.

I assume it would be with Instantiate and Invoke, I don't think this is the answer but here is an Idea I have: Instead of normally spawning, like: InvokeRepeating("Spawnenemy", 5) would InvokeRepeting("Spawnenemy", Variablethatgoesup) work?

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 Chronos-L · Mar 17, 2013 at 06:36 AM 0
Share

You are on the right track. You also need to know how to stop InvokeRepeating before re-invoke the same function.

avatar image 3 · Mar 17, 2013 at 08:00 PM 0
Share

Thanks, everyone's answered worked, just picked the one I used. Thanks again,

3 Replies

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

Answer by sparkzbarca · Mar 17, 2013 at 07:18 AM

lots of ways to do this of course but if you want to link spawn rate to time

assuming you want the spawn to be at the same moment (that is spawn 20 in 1 frame) then you can do

every minute spawn x where x equals

spawnrate = Time/60 (time is a built in object, it returns the time in seconds since game start)

 if(Time/60 == 0)
 for(int x = 0; x < spawnrate; x++)
 spawn();


if you'd like you can just spawn 1 every x seconds but give the rate a curve so you spawn more often but you spawn only 1 each time something like

 SpawnRate = 60; // we'll start out spawning once a minute
 
 if (Time/SpawnRateInSeconds == 0)
 spawn();

now you write a script to reduce spawnrate by so much every so long that'd look something like

 if(Time/60 == 0) // every minute were going to modify spawn rate
 SpawnRate -= 5;

so in this case we'll spawn 1 the first minute and by minute 6 we'll be spawning 2 every minute.

The main issue with this method is it's going to get basically impossible because the growth is goign to ramp hard

that is to say when the spawnrate is 60 seconds taking 5 seconds off wont matter much.

but later on when its 10 seconds taking 5 seconds off will basically double the spawn rate making it ramp up hugely. Your basically looking at exponential growth here. you'll want to make sure also that you down let spawn rate drop below 0 (or you'll have bizzare behavior). I put that in in case you acutally wanted this kind of large exponential curve.

You could have a non curved growth as well you could do that by using division by the rate soemthing like

 if(Time/60) //again we'll assum we want to change once a minute
 spawnrateinseconds = spawnrateinseconds/4
 
 or generaly
 .... = spawnrate/x 
 where x is the rate

so if its 4 we'll make it so every minute it spawns 25% faster/ but it'll be a constant slope where as we get to lower spawn times we'll also take less off the timer meaning you wont get these large spikes at the end.

MARK AS ANSWERED

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
5

Answer by whydoidoit · Mar 17, 2013 at 07:35 AM

A very useful built in feature of Unity is the AnimationCurve which despite it's name - you can use for things apart from animation!

Add a variable of type AnimationCurve to your script, then use the animation editor to create a curve which relates time on the x axis to some other value on the Y axis - in your case the delay between spawning enemies. In the inspector it might look a bit like this:

alt text

The animation curve editor is a bit fiddly - but once you get the hang of it its fine. alt text

Then what you need is a coroutine which uses a value from the curve:

In C#

    public AnimationCurve curve;

    IEnumerator SpawnEnemies()
    {
          while(true)
          {
               yield return new WaitForSeconds(curve.Evaluate(Time.timeSinceLevelLoaded));
               //Spawn your enemy
          }

    }



screen shot 2013-03-17 at 07.32.08.png (15.9 kB)
screen shot 2013-03-17 at 07.31.54.png (31.5 kB)
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 robertbu · Mar 17, 2013 at 07:44 AM

I looked at a few curves, but nothing seemed to fit your specification. So here is a more literal implementation (untested):

 var startTime : float;
 var nextTime : float;
 
 function Start () {
     startTime = Time.time;
     nextTime = Time.time;
 }
 
 function Update () {
 
     if (Time.time > nextTime)
     {
         var minutes : float = Mathf.Floor((Time.time - startTime) / 60.0);
         nextTime = Time.time + 60.001 / (minutes + 1.0);
         // Spawning code goes right here
     }
 }

It will spawn 1 the first minute, 2 the second minute, 3 the third...

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

13 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

Related Questions

How do I control where enemies spawn? 1 Answer

How to make my enemies spawn at spawn point, but not randomly? 1 Answer

How to Spawn after checking if the clones are destroyed. 1 Answer

Have a spawning issue on iOS 0 Answers

how to destroy a object only in game and not the prefab 2 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