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 /
  • Help Room /
avatar image
0
Question by Creative Inventory · Oct 01, 2015 at 10:00 PM · spawnwave

How to add a wave in my spawn script

Okay basically I have a 2D game which I would like to spawn an prefab in cube, I have a spawning script but I would like to make this script into a wave spawning script. Where at a certain time the waves becomes faster then faster and so on. If there is anyway of doing this or an example you can give that would be amazing and i would appreciated it so much! Thank you! Here is my script:

 using UnityEngine;
 using System.Collections;

 public class Atempt1 : MonoBehaviour {

 public GameObject ObjectToSpawn;   
 public float RateOfSpawn = 1;
 public float SpawnDelay = 20f;
 private float nextSpawn = 0;
 
 // Update is called once per frame
 void Update () {           
     
     if (Time.time > nextSpawn) {
         nextSpawn = Time.time + RateOfSpawn;
     }
 }
     void Start(){
             Invoke("Spawn", SpawnDelay);
         }
         
         void Spawn () {           
             // Random position within this transform
             Vector3 rndPosWithin;
             rndPosWithin = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
             rndPosWithin = transform.TransformPoint(rndPosWithin * .5f);
             Instantiate(ObjectToSpawn, rndPosWithin, transform.rotation);  
             
             Invoke("Spawn",SpawnDelay);
         }

}

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
0

Answer by bubzy · Oct 01, 2015 at 10:31 PM

you can try something like this

WARNING this quickly gets out of hand, so its worth checking the numbers or having some method of seeing if the previous wave was cleared first.

 using UnityEngine;
 using System.Collections;
 
 public class spawner : MonoBehaviour {
 
     // Use this for initialization
     public GameObject spawnThing;
     int spawnLevel = 1;
     int spawnCount;
     float spawnDelay = 3f; //seconds between waves
     float minSpawnDelay = 0.1f; // the shortest time between waves possible
 
     float currentDelay;
 
 
     void Start () {
         currentDelay = Time.time + spawnDelay;
     }
     
     // Update is called once per frame
 
     void Update () {
     if (Time.time > currentDelay) {
             for(int i = 0; i < spawnLevel; i++)
             {
                 spawnEnemy();
             }
             spawnLevel ++; //increase the spawn level
             spawnDelay -= spawnLevel/10; //just an approximation, this will take 0.1 seconds off per level
             if(spawnDelay < minSpawnDelay)
             {
                 spawnDelay = minSpawnDelay; // this ensures that the spawn delay does not drop below this time.
             }
             currentDelay = Time.time + spawnDelay;
         }
     }
 
     void spawnEnemy()
     {
         GameObject temp = Instantiate (spawnThing, transform.position, Quaternion.identity) as GameObject;
         //do whatever you want with temp
         Destroy (temp, 2);// this for debug to make sure it doesnt get overrun.
     }
 }
 

hope it helps :)

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

30 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

Related Questions

how can I add a maximum of enemies to be generated for each wave? 0 Answers

Wave spawner script 2 Answers

Spawning Bug. 1 Answer

Can someone help me add a wave to my Spawner script!!! 0 Answers

Spawning random objects not working 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