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 Socterean · Feb 24, 2015 at 11:24 AM · spawnspeedenemies

How To: spawn succesive waves of enemies increasing their speed

Hi guys!

I am experimenting with one tutorial games provided by Unity: Space Shooter http://unity3d.com/learn/tutorials/projects/space-shooter; And I want to make minor changes to it so I can learn how to further develop this project by adding extra features and so on.

The question is, how could I modify the script, so when a player reaches 1000 points for example to spawn more enemyes, to increase the speed of them and things like that.

 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour
 {
     public GameObject hazard;
     public Vector3 spawnValues;
     public int hazardCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
 
     void Start ()
     {
         StartCoroutine (SpawnWaves ());
     }
 
     IEnumerator SpawnWaves ()
     {
         yield return new WaitForSeconds (startWait);
         while (true)
         {
             for (int i = 0; i < hazardCount; i++)
             {
                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate (hazard, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds (spawnWait);
             }
             yield return new WaitForSeconds (waveWait);
         }
     }
 }

Thanks in advance!

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 sniper43 · Feb 24, 2015 at 11:30 AM 0
Share

Increase the hazardCount variable when play score reaches a threshold...

So reference the script and in the scorecounter class increase the hazardCount in the update after checking .

It being a coroutine SHOULDN'T be an issue.

avatar image melkorinos · Feb 24, 2015 at 11:34 AM 0
Share

Hazard count is the amount of enemies that are spanwed. I picture in another script there must be a points value, which you can make it static (if it is not already). After you made it static you can access it in your spawner script just by ClassName.points and do an if clause , in a update function that you will create, to increase hazard count depending on it. To increase spawning speed, simply make spawnWait smaller.

To increase the speed of the enemies you will do the same things in the enemy behaviour script.

1 Reply

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

Answer by Rs · Feb 24, 2015 at 12:14 PM

I'll do some assumptions:

  1. You have a PlayerType class that has a public variable that represents points.

  2. You have a Player game object in your scene which holds the PlayerType script among its components.

  3. You dragged your Player instance into the public field called myPlayer of your GameController component.

The following approach does not work if you have an infinite game.

 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour
 {
     
     public PlayerType myPlayer; // Get your player's reference here
     
     public GameObject hazard;
     public Vector3 spawnValues;
     public int hazardCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
     
     void Start ()
     {
         StartCoroutine (SpawnWaves ());
     }
     
     IEnumerator SpawnWaves ()
     {
         yield return new WaitForSeconds (startWait);
         while (true)
         {
             for (int i = 0; i < hazardCount; i++)
             {
                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate (hazard, spawnPosition, spawnRotation);
                 
                 // Easy way: Make your list of conditional spawning here
                 if(myPlayer.points < 999)
                 {
                     yield return new WaitForSeconds (spawnWait); // normal spawnWait
                 }
                 else if(myPlayer.points > 1000 && myPlayer.points < 1999)
                 {
                     yield return new WaitForSeconds (spawnWait * 0.8f); // Quicker spawnWait
                 }
                 else if(myPlayer.points > 2000 && myPlayer.points < 4999)
                 {
                     yield return new WaitForSeconds (spawnWait * 0.7f); // Even Quicker spawnWait
                 }
                 // go on with how many conditions you wish
                 else // last else is just for defensive programming, meaning it takes the cases that should not happen, like player points being < 0
                 {
                     yield return new WaitForSeconds (spawnWait);
                 }
                     
                 yield return new WaitForSeconds (waveWait);
                 }
             }
         }
         
         

The other way, if you have an infinite game where difficulty constantly raises depending on player points, you just a need a simple formula like this to make it grow linearly with the maximum difficulty when the player reaches 10000 points and a waiting time of at least 3f (when starting) seconds and minimum (most difficult) 0.1f seconds:

 spawnWait = max(0.1f, 3f - ( ((float)myPlayer.points)/10000f ) * 3f );

The spawnWait function is similar to what you see here but in the code it will be limited between 3 (assuming your player points will always be > 0) and 0.1.

You can also affect the speed of your hazards with the same approach.

Hope this 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

22 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

Related Questions

Spawing Enemies works but works to good 2 Answers

Enemy spawn issues 1 Answer

How to store enemies spawn schedule for each scene ? 1 Answer

Increase number of enemies spawned per level 0 Answers

Changing the spawn rate 0 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