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 quantumarchi · Dec 13, 2011 at 08:39 AM · instantiatetimespawnienumeratorstartcoroutine

how do i make my spawn system spawn every 2 seconds then wait 10minutes?

so i have made mob generator just fine and they spawn but they only spawn once i wonder how i can allow them to spawn every 2 seconds then stop spawning after 10minutes wait 10minutes then spawn a new wave, at the moment it set to random range and selects a mob at random thanks for any help

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

public class MobGenerator : MonoBehaviour { public enum State { Idle, Initialize, Setup, SpawnMob } public GameObject[] mobPrefabs; //array to hold all mob prefabs public GameObject[] spawnPoints; //array of spawn points

 public State state;
 
 void Awake() {
     state = MobGenerator.State.Initialize;
 }
 // Use this for initialization
 IEnumerator Start () {
     while(true) {
         switch(state) {
         case State.Initialize:
             Initialize();
                 break;
         case State.Setup:
             Setup();
                 break;
         case State.SpawnMob:
             SpawnMob();
                 break;
             
         }
         
         yield return 0;
     }
 }
 private void Initialize() {
     
     if(!CheckForMobPrefabs())
         return;
     if(!CheckForMobSpawn())
         return;
     
     state = MobGenerator.State.Setup;
 }
 
 private void Setup() {
     state = MobGenerator.State.SpawnMob;
 }
 
 private void SpawnMob() {
     
     GameObject[] avaliable = AvailableSpawnPoints();
     
     for(int cnt = 0; cnt < avaliable.Length; cnt++) {
         GameObject SpawnMob = Instantiate(mobPrefabs[Random.Range(0, mobPrefabs.Length)],
                                     avaliable[cnt].transform.position,
                                     Quaternion.identity
                                     ) as GameObject;
         SpawnMob.transform.parent = avaliable[cnt].transform;
     
     state = MobGenerator.State.Idle;
 }

}

 //check for mobs
 private bool CheckForMobPrefabs() {
     if(mobPrefabs.Length > 0)
         return true;
     else
         return false;
 }
 
 private bool CheckForMobSpawn() {
     if(spawnPoints.Length > 0)
         return true;
     else
         return false;
 }
 
 private GameObject[] AvailableSpawnPoints() {
     List<GameObject> avaliable = new List<GameObject>();
     
     for(int cnt = 0; cnt < spawnPoints.Length; cnt++) {
         if( spawnPoints[cnt].transform.childCount == 0) {
             avaliable.Add(spawnPoints[cnt]);
         }
     }
     
     return avaliable.ToArray();
 }

}

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

3 Replies

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

Answer by Oninji · Dec 13, 2011 at 06:51 PM

Add a canSpawn var, and switch it between true to false. It would look like something like that.

 var canSpawn : boolean = true;
 var spawnTime : float = 600.0;
 var spawnClockStart : float = 0.0;
 var spawnClockEnd : float = 1.0;
 var spawnClockCur : float = 0.0;
 var lerpAmt : float = 0.0;
 
 function Update(){
 lerpAmt += Time.deltaTime / spawnTime;
     spawnClockCur = Mathf.Lerp (spawnClockStart, spawnClockEnd, lerpAmt);
 
   if(canSpawn &&
   spawnClockCur <= 1.0){
   canSpawn = false;
   lerpAmt = 0.0;
   spawClockCur = 0.0;
   }
   if(!canSpawn &&
   spawnClockCur <= 1.0){
   canSpawn = true;
   lerpAmt = 0.0;
   spawnClockCur = 0.0;
   }
 }

With this, just use the "canSpawn" as the condition to spawn your ennemies.

PS: I'm aware it's not a "Clean" code, it's just to show the principle of it.

Comment
Add comment · Show 2 · 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 quantumarchi · Dec 13, 2011 at 10:43 PM 0
Share

thanks now i didnt use code but it definitely help me understand what i was going for thanks very much works perfectly now

avatar image Oninji · Dec 14, 2011 at 03:30 AM 0
Share

No Problem :P Don't forget to choose as answer so the topic is set to "Answered" :P

avatar image
0

Answer by eaperez · Dec 13, 2011 at 10:07 PM

Hello, i see that you use an FSM here. You can make a new State called "WaitTenMins" or something like that and set it after spawning the mobs. In the function you can try this:

 public float waitTime = 10F * 60F; //ten minutes
 private float nextWaveTime = 0.0F;
 
 void WaitTenMins() {
     if (Time.time > nextWaveTime) {
         state = MobGenerator.State.SpawnMobs;
     }    
 }


Of course you will need to set the nextWaveTime to Time.time + waitTime before you set the state to "WaitTenMins". Hope that helps.

Comment
Add comment · Show 3 · 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 Bunny83 · Dec 14, 2011 at 04:26 AM 0
Share

10$$anonymous$$ == 10*60sec == 600.0f ;)

avatar image quantumarchi · Dec 14, 2011 at 07:27 AM 0
Share

oh yes i actually did do this but i did take the first guys answer but thanks very much added a few extra floats and a bool ill post it later :P

avatar image eaperez · Dec 14, 2011 at 01:08 PM 0
Share

my bad, edited to work for the ten $$anonymous$$utes ins$$anonymous$$d of 10 seconds XD. thanks.

avatar image
0

Answer by joeybbb · Dec 13, 2011 at 11:05 PM

I may be able to help, as I have done this in my game, but I use java script. Do you mind totally changing your script to javascript???

Comment
Add comment · Show 1 · 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 syclamoth · Dec 13, 2011 at 11:06 PM 1
Share

Now why would you ever want to do that?

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make enemy spawn only if the previous one spawned it out of the way. 0 Answers

spawn timer problem 2 Answers

Instantiate 1 at a time at each transform in array 1 Answer

How to make something happen over time? 1 Answer

Instantiate over time? 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