I have the following two scripts controlling the behavior of my bubble game objects. The intention is to have the bubbles appear randomly, then inflate (as if blown up) at random rates of speed to random sizes (some people blow bigger bubbles faster than others).:
BlowBubble.js
var startScale = Vector3.zero;
var endScaleMax = Vector3(5, 5, 5);
var endScaleMin = Vector3.one;
var t = 0.0;
while (t<1.0) {//Bubbles are 'blown up' based on speed and size variables they receive from the BubbleSpawnerS.js
t += Time.deltaTime * BubbleSpawnerS.SPEED;
transform.localScale = Vector3.Lerp(startScale, BubbleSpawnerS.RANDOM1, t);
yield;
}
BubbleSpawnerS.js
static var TOTBUB = 0;
var bubble : Transform;
var MaxBubbles = 10;
var MinTime = .1;
var MaxTime = .8;
var MinX = 0;
var MaxX = 39;
var MinY = 0;
var MaxY = 14;
static var RANDOM1;
static var SPEED = .1;
var minscale = 1;
var maxscale = 5;
var maxspeed = 1;
var minspeed = .1;
var superz = Random.Range(minscale,maxscale);
var randomgen = Vector3(superz,superz,superz);
RANDOM1 = randomgen;
function Start ()
{
while (true) {
if (TOTBUB < MaxBubbles) {//Script spawns bubbles at random positions at random points in time if we are not at maximum amount of bubbles
yield WaitForSeconds(Random.Range(MinTime, MaxTime));
var randomPos = Vector3 (Random.Range(MinX, MaxX), Random.Range(MinY, MaxY), 0);
Instantiate(bubble, randomPos, Quaternion.identity);
TOTBUB++;
//Script changes random static variables here that are read by BlowBubble.js which is attached to the bubble objects being spawned
//My intent is to have each newly spawned bubble object take this set of random variables and grow with them (like the bubbles are being blown up at different speeds to different sizes).
//My problem is that each bubble receives these updated variables each time a new bubble is spawned.
//So if a bubble is still growing due to a randomly slow growth time, then when a new bubble is spawned, both the new bubble and any other bubble that is still growing receives these variables.
//This results in my bubbles animating in a jittery fashion, each time a new bubble spawns all of the growing bubbles 'jump' to coincide with the new variables they are receiving
//This behavior stops on the bubble if it reaches it's maximum size before another spawn cycle
//Note that if I simply set the speed variable to a fixed fast speed that none of this becomes a problem, only my game isn't fun if just one bubble is spawning at a time.
var superz = Random.Range(minscale,maxscale);
var randomgen = Vector3(superz,superz,superz);
RANDOM1 = randomgen;
var speed = (Random.Range(minspeed, maxspeed));
SPEED=speed;
}
yield;
}
}
When the bubbles are spawned, they are passed static variables to control inflation speed and size.
One bubble is spawned at a time, but additional bubbles are spawned as previous bubbles are still growing.
If a bubble is still growing and another bubble is spawned, these static variables are read by the bubbles that are already growing and they ‘jump’ in size in a jittery fashion because of it. I want to eliminate this jittery jumping of size.
How do I make the bubbles that are spawned keep their initial instructions to grow to a size dictated by the static variable at the time of them spawning?
Would something involving private variables be plausible? I’m not too sure how they work, but couldn’t I set a private var = static var as the static var comes into the script for the first time? Would this make just the one bubble keep it’s original variables?