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 09, 2015 at 11:42 AM · bugspawnthanks

Spawning bug problem

Okay basically i have a 2d game that spawns enemies from the left side and the right side of main camera's view. I have included a spawn delay so my enemies will come in after a certain time. I also have a difficulty factor set to "0.9". My problem is that after a long time of playing the game my spawner begins to bug. It ignores the spawn delay and spawns more and more enemies at a time (it's kind of creepy!)

alt text

If anyone can help me please do! Thank you!!!

Here's my script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

  [System.Serializable]
  public class WaveAction
  {
    public string name;
    public float delay;
    public Transform prefab;
    public int spawnCount;
    public string message;
  }

 [System.Serializable]
 public class Wave
  {
       public string name;
       public List<WaveAction> actions;
   }



 public class WaveGenerator : MonoBehaviour
 {
     public float difficultyFactor = 0.9f;
     public List<Wave> waves;
     private Wave m_CurrentWave;
     public Wave CurrentWave { get {return m_CurrentWave;} }
     private float m_DelayFactor = 1.0f;
     public GameObject ObjectToSpawn; 
 
   IEnumerator SpawnLoop()
   {
     m_DelayFactor = 1.0f;
     while(true)
     {
         foreach(Wave W in waves)
         {
             m_CurrentWave = W;
             foreach(WaveAction A in W.actions)
             {
                 if(A.delay > 0)
                     yield return new WaitForSeconds(A.delay * m_DelayFactor);
                 if (A.message != "")
                 {
                     // TODO: print ingame message
                 }
                 if (A.prefab != null && A.spawnCount > 0)
                 {
                     for(int i = 0; i < A.spawnCount; i++)
                     {
                         
                         // 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);  
                         
                     }
                 }
             }
             yield return null;  // prevents crash if all delays are 0
         }
         m_DelayFactor *= difficultyFactor;
         yield return null;  // prevents crash if all delays are 0
     }
 }
 void Start()
 {
     StartCoroutine(SpawnLoop());
 }
 

}

I tried changing the m_DelayFactor = 1.0f; to m_DelayFactor = 5.00f; but it does the same but slowly, i also tried it like this: m_DelayFactor=0.1f; but it was larger than before the original (and creepier) Which I do not want! Thank you again, I appreciated it very much.

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 dkjunior · Oct 09, 2015 at 04:27 PM

With each wave it multiplies the delay factor by difficultyFactor, so as long as difficultyFactor is less than 1.0, the delay factor will be getting smaller and smaller eventually diminishing to zero. That would result in behavior that you observe (spawning more and more objects with literally no delay). I thought that was the point of difficulty factor (the game gets more difficult over time).

If you want your enemies to be spawned at a constant rate defined by difficultyFactor, move the m_delayFactor assignment to the beginning of the coroutine:

  IEnumerator SpawnLoop()
    {
      m_DelayFactor = difficultyFactor;
 ...
 }
Comment
Add comment · Show 4 · 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 Creative Inventory · Oct 09, 2015 at 05:43 PM 0
Share

Thank you for answering my question but I'm confused could you please explain a bit more? Thank you once again @dkjunior

avatar image dkjunior Creative Inventory · Oct 10, 2015 at 12:47 AM 0
Share

With your code above, the delay factor gets smaller with each wave:

Wave 1: m_delayFactor = 1.0 Wave 2: m_delayFactor = 1.0 x 0.9 = 0.9 Wave 3: m_delayFactor = 1.0 x 0.9 x 0.9 = 0.81 ... and so on.

Eventually it will be zero, resulting in enemies spawning without delay. This is what you observe after playing the game for some time.

What is the desired behavior? If you want the game to get faster with time, then your code is actually correct. You may just want to tweak the difficulty factor (say, change it 0.95) if it's too fast.

If you want your enemies to be spawned at a constant rate, move the m_delayFactor *= difficultyFactor; line out of the main loop so that it doesn't change with each wave.

Hope this helps.

avatar image Creative Inventory · Oct 10, 2015 at 07:38 AM 0
Share

Thank you for answering back so quick. I don't really want a difficulty factor in my script but when i took it out and saved it it doesn't spawn anything!! i understand what you mean know on which i am very grateful for that, but I just want it to spawn normally each wave. Thank you once again!! @dkjunior

avatar image dkjunior Creative Inventory · Oct 10, 2015 at 04:31 PM 0
Share

If you just want it to spawn normally each wave, keep your original code and set difficulty factor to 1. Glad I was able to help. Please accept the answer if it addressed your need :)

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

31 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

Related Questions

Spawning Bug. 1 Answer

UNET : random bugs with spawning 0 Answers

Help with Fixed Joint , where the joint has a weird behavior. 0 Answers

Rigidbody / Animator Moving Y Position - bug? 1 Answer

Particle System not showing anything in its Component separated editor? 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