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 Rick74 · Aug 27, 2013 at 04:45 AM · javascriptinstantiateenemy spawn

Having trouble with an enemy spawn script

I'm trying to create an enemy spawn script that starts off slowly spawning enemies, and then slowly ramps up after each minute...as well as has a "large wave" of enemies at a certain time.

Everything runs smoothly for the first minute, and it feels like it transitions nicely into the second minute where the level increases, but for some reason there seems to be two seperate timers for the "SecondlevelSpawn". And it will then just start spitting out 1 extra wave of Second level spawns.

I've been at this for about two days now, and I can't find out why this is happening. Hopefully another set of eyes on this will help!

 #pragma strict
 
 static var playerDamage:         float = 0;                //records the damage the player has taken
 
 var waveActive:                    boolean = true;            //toggles for controlling the wave
 var spawnEnemies:                 boolean = true;            //
 var bigSpawn:                    boolean = false;
 var firstWaveActive:             boolean = true;
 var secondWaveActive:             boolean = false;
 
 var healthCount:                 int = 10;                //players health count
 
 var waveText:                     UILabel;                //ui label 
 var healthText:                 UILabel;
 
 var waveLevel:                    int = 0;                //current wave we are on

 var enemyPrefabs:                 GameObject[];            //array that contains all the enemy types
 var enemyPrefabs02:                GameObject[];                
 var alienSpawnPoints:             Transform[];            //contains the spawn points
 var respawnMinBase:                float = 3.0;            //used to ramp up the amount of spawns
 var respawnMaxBase:             float = 10.0;            //
 private var respawnMin:         float = 3.0;            //
 private var respawnMax:         float = 10.0;            //
 
 var difficulty:                 int = 0;
 var seconds:                     int = 0;
 var minutes:                    int = 0;
 
 
 
 
 function Start () 
 {
                 
 }        
 
 function Update () 
 {    
     Clock ();
     
     if (waveActive)
     {
         spawnEnemies = false;
         FinishWave ();
     }
     if ( minutes >= 1 && firstWaveActive )
     {
         firstWaveActive = false;
         secondWaveActive = true;
         print ( "swapping over to second wave level");
         waveActive = true;
     }
     if ( waveLevel == 09 && secondWaveActive )
     {
         secondWaveActive = false;
         bigSpawn = true;
         waveActive = true;
     }
 }
 
 function SetNextWave ()                                                //prepares the values for the next wave (how many enemies will be spawn)
 {
     waveLevel ++;                                                       //ups the wave level
 }
 
 function StartNewWave ()
 {
     UpdateHUD ();                                                    //updates the gui
     NewSpawnEnemy ();                                                //spawns new enemy
     waveActive = true;                                                //sets the toggle to spawn enemies
     spawnEnemies = true;
 }
 
 function UpdateHUD ()
 {
     waveText.text = "Wave: " +waveLevel;                            //updates the current waveLevel to the wave GUI
     healthText.text = "Health " +healthCount;                        //updates the current healthLevel to the health GUI
 }
 
 function FinishWave ()
 {
     waveActive = false;    
     var i: int;    
                                                     
     if ( minutes <= 1 && firstWaveActive && !secondWaveActive )
     {
         spawnEnemies = false;
         i = Random.Range (20, 30);
         print ("under 1 minute, waiting for "+i);
         yield WaitForSeconds (i);                                        //waits for the the intermission time to pass
         SetNextWave ();                                                    //sets the next wave command
         StartNewWave ();            
     }
     if ( minutes >= 1 && !firstWaveActive && secondWaveActive)
     {
         spawnEnemies = false;
         i = Random.Range (12, 18);
         print ("over 1 minute, waiting for "+i);
         yield WaitForSeconds (i);                                        //waits for the the intermission time to pass
         SetNextWave ();                                                    //sets the next wave command
         StartNewWave ();        
     }
 }
 
 function NewSpawnEnemy ()
 {
     if ( minutes <= 1 && firstWaveActive && !bigSpawn )
     {
         FirstLevelSpawn ();
         print ("first level spawn happening");
     }
     if ( minutes >= 1 && !bigSpawn && secondWaveActive && !firstWaveActive )
     {
         SecondLevelSpawn ();
     }
     if ( bigSpawn && !secondWaveActive )
     {
         BigSpawn ();
         print ("big spawn happening");
     }
 }
 
 function FirstLevelSpawn ()
 {
     var i = difficulty;
     for (; i >= 0; i--)
     {
         var r = Random.Range (1, 2);
         var enemyChoice = Random.Range (0, enemyPrefabs.Length);
         var spawnChoice : int;    
         spawnChoice = Random.Range (0, alienSpawnPoints.Length);
         Instantiate (enemyPrefabs[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
         yield WaitForSeconds (r);
     }
     print ( "first level spawn finished");
 }    
     
     
 
 function SecondLevelSpawn ()
 {    
     var i = difficulty;
     for (; i >= 0; i--)
     {
         var enemyChoice = Random.Range (0, enemyPrefabs02.Length);
         var spawnChoice : int;    
         if ( enemyChoice == 1 )
         {
             var a = 1;
             for (; a >= 0; a--)
             {
                 spawnChoice = Random.Range (0, alienSpawnPoints.Length);
                 Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
             }
         }
         if ( enemyChoice == 0 )
         {
             spawnChoice = Random.Range (0, alienSpawnPoints.Length);
             Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
         }
     }
     print ( "second level spawn finished" );
 }
 
 function BigSpawn ()
 {
     print ("Big Spawn!");
     var i = 3;
     for (; i >= 0; i--)
     {
         var enemyChoice = Random.Range (0, enemyPrefabs02.Length);
         var spawnChoice : int;    
         if ( enemyChoice == 1 )
         {
             var a = 1;
             for (; a >= 0; a--)
             {
                 spawnChoice = Random.Range (0, alienSpawnPoints.Length);
                 Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
             }
         }
         if ( enemyChoice == 0 )
         {
             spawnChoice = Random.Range (0, alienSpawnPoints.Length);
             Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
         }
         print ( "big spawn loop "+i);
     }
     bigSpawn = false;
     secondWaveActive = true;
     print ("big spawn finished");
 }
 
 function Clock ()
 {
     seconds = Time.time % 60;
     minutes = Time.time / 60;
 }
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 getyour411 · Aug 27, 2013 at 06:55 AM 0
Share

It's kinda bouncing around and hard for me to follow but I'm guessing one issue is that you've declared seconds and $$anonymous$$utes as Int but are dividing by a float (time.time), don't know how UnityScript handles that. In one of all those tests you have for $$anonymous$$ute>=1 or $$anonymous$$ute

 var r = Random.Range (1, 2);

will always return 1 since a Random.Range on Integers excludes the max, though I see nowhere that matters in your code per se, just pointing it out.

avatar image Rick74 · Aug 27, 2013 at 06:08 PM 0
Share

Wel, when I test it, I put a ton of print statements in it, to see what's happening when. It seems to double up on the second wave. Other than that, it seems to be working ok. Thanks for the tip on Random.Range. So you can't see anything in there that would make it play out

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rick74 · Aug 27, 2013 at 06:16 PM

if anyone is interested, I've solved this (at least for now) by adding in a empty float private var waveEndTime.

waveEndTime = Time.time + waveLength;

with this, I've added it to my update so now nothing will happen unless Time.time >= waveEndTime.

so my update now looks like this;

 function Update () 
 {    
     Clock ();
     if (waveActive)
     {
         if ( Time.time >= waveEndTime )
         {
             spawnEnemies = false;
             FinishWave ();
             print ( "shutting off wave");
         }
     }
     if ( minutes >= 1 && firstWaveActive )
     {
         firstWaveActive = false;
         secondWaveActive = true;
         print ( "swapping over to second wave level");
         waveLength = 5;
     }
     if ( waveLevel == 09 && secondWaveActive )
     {
         secondWaveActive = false;
         bigSpawn = true;
         print ( "swapping over to big spawn");
         waveLength = 40;
         FinishWave ();
     }
     if ( minutes >= 4 )
     {
         difficulty = minutes - 2;
     }
 }

I feel this has slowed things down to a reasonable pace that I can somewhat control now. However, I do feel like this whole spawner could have been made much more efficiently, and less complicated.

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

16 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

Related Questions

Putting a limit on the amount of times you can instantiate an Enemy 1 Answer

How to spawn objects but set a cap of how many can be alive at the same time? 0 Answers

Instantiating at different rates 1 Answer

how to make enemy clone work independently? 1 Answer

Unity not recognizing Instantiate? JavaScript 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