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.
I tried yield waitforseconds in a few different places with my makebubbles function and still was not getting the desired results 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…?
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. That is why I was posting originally. 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.
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.