Slow down Instantiate?

Hey guys!

I am working on a project where when the player moves I create bubbles which are simply a gameobject with an animator that plays a nice bubble animation.

I want them to follow the player and they do! The current ‘issue’ is that they spawn so fast, they are too close together. I am trying to make it so they are more spaced apart. Basically I have an empty array which holds the created gameobjects, once the array is full it no longer makes new bubbles. Everything is working great, they are just appearing too close together and that is because they are getting called all at the same time from my update function. In the update I check if the player is launching, and if that is true I call my makeBubbles() function, which is as follows.

function makeBubbles()
{
   bubbleAudio.SetActive(true);
       
   if(bubbleCount < bubbleArray.Length)
   {
       var bubbs : GameObject = Instantiate(bubbleEffect, this.gameObject.transform.position, new Quaternion(0,0,0,0));
       bubbleArray[bubbleCount] = bubbs;
       bubbleCount += 1;
   }

}

Thanks guys! I have already tried using some kind of for loop but that ended up making 6000 more bubbles than I wanted… lol I also tried setting it up so the instantiate start position is the difference between the player and the last bubble but that was also not working well for me.

Any input would be greatly appreciated!

Thanks again!

Coroutines!

1 Like

I tried yield waitforseconds in a few different places with my makebubbles function and still was not getting the desired results :frowning: Do you have any exact suggestions for implementing a coroutine?

The main problem is… I am calling the function from Update… so I don’t think I am able to use a coroutine…?

UPDATE!

I believe I got it! By added a boolean and a yield wait for seconds! My checks in the update now say ‘if launcing && !makingBubble’

function makeBubbles()
{
   bubbleAudio.SetActive(true);
   makingBubble = true;
   
   
   if(bubbleCount < bubbleArray.Length)
   {   
       var bubbs : GameObject = Instantiate(bubbleEffect, this.gameObject.transform.position, new Quaternion(0,0,0,0));
       bubbleArray[bubbleCount] = bubbs;
       bubbleCount += 1;
   }
   
   yield WaitForSeconds(.035);
   
   makingBubble = false;

}
1 Like

Are the bubbles just asthetic? You might want to consider using a particle system that plays/ pauses with the players movement.

1 Like

It works, but it’s going to kill your performance. This is a better way.

void Start () {
    StartCoroutine(MakeBubbles());
}

IEnumerator MakeBubbles()
{
   while (true) {
      Instantiate(bubbleEffect, transform.position, Quaternion.identity);
      yield return new WaitForSeconds(.035f);
   }
}
1 Like

The main problem with doing it via the Start method is I need to call the bubbles via the Update method and as far as I know, Coroutines don’t work in the Update method. :frowning: That is why I was posting originally. :frowning: It doesn’t seem to have much of an impact on performance. This is a smaller 2d project.

Is there a way to do this and hooking it into my player input?

Originally yes, I was using a particle system. But the person that I am programming this project for wanted me to use an animation instead. Originally I was opposed to the idea since this is exactly why particle systems exist… but boy was I wrong once I had his animation hooked up! It looked much better than my randomized computer generated bubbles.

You start the Coroutine in Start. Then it will continue to run on its own.

The other way to do this is to set up a timer in Update.

public float interval;//this is the waiting time between the creation of bubbles

void Start(){
    StartCoroutine("MakeBubbles", 0);//I start coroutine here, no delay since the first one should be immediate.
}

IEnumerator MakeBubbles(float t){
    yield return new WaitForSeconds(t);
    //make bubble, whatever you have done so far to instantiate the bubble and add to tracking integer
    if(/*bubble amount not yet  reached*/){
        StartCoroutine("MakeBubbles", interval);//call coroutine again, but this time apply time interval
    }
}

Although my assumption of your situation so far is that you have a max amount of bubbles you want to spawn, which upon a repeat reading of your original post, does not seem to be the case. If you indeed have a moving character leaving a bubble path, and if the bubble path is merely aesthetic contributing nothing to gameplay, then I would agree with @SarfaraazAlladin that a particle effect would be a better way to go about this.

You can start Coroutines from Update. You do have to be careful that you’re not starting a new coroutine every frame, but that’s it.

Fair enough :slight_smile:

In that case, do yourself a favor and implement an object pool system before you go too deep. You’ll thank yourself later.