A problem involving the While statement

In my level, I have Lava spouts the player has to jump over. These spouts ‘gush’ upwards and then gradually fade back down to its constant bubbling state. If the player miss times their jump and lands in the spout they ‘die’ and get repspawned.

The way i’ve gone about it is to use a particle system with a world particle collider component for the collision detection and a script that controls the world velocity to simulate the ‘gushing’ and fade off using a coroutine in the script. I’ve got a set of variables so that i can duplicate the lava spout and change timings depending on the difficulty i want.

The lava light is there to give a visual clue to the player that the spout is happening and gives them a choice about jumping or not.

Anyways, here’s the script:

var lavaSpout : GameObject;
var lavaLight : GameObject;
var startTime : float = 1; //time started after initialisation
var repeatTime : float = 10; //how often repeated
var gushTime : float = 4; //time multiplier to gush
var spoutTime : float = 1; //time at full spout
var fadeTime : float = 5; // time multiplier to fade back
var spoutHeight : float = 10; //height of spout required using particle world velocity

function Start()
{
InvokeRepeating("spoutstart", startTime, repeatTime);
}

function spoutstart()
{
spout();
}

function spout()
{
    while (lavaSpout.particleEmitter.worldVelocity.y < spoutHeight && lavaLight.light.intensity < 6)
    {
	lavaSpout.particleEmitter.worldVelocity.y += Time.deltaTime *(gushTime);
	lavaLight.light.intensity += Time.deltaTime *(gushTime);
	yield;
    }
    yield WaitForSeconds (spoutTime);
    yield SpoutFade();

}

function SpoutFade()
 {
   while (lavaSpout.particleEmitter.worldVelocity.y > .05 && lavaLight.light.intensity > 0) 
   {
	lavaSpout.particleEmitter.worldVelocity.y -= Time.deltaTime *(fadeTime);
	lavaLight.light.intensity -= Time.deltaTime *(fadeTime);
	yield;
   }
   yield;

}

The script works fine and is just about giving the desiered affect, i say ‘just,’ because the problem i’m encountering is with the while element, the spout height or light intensity only ever reaches the lowest number of the two.

So if the spoutHeight is set at 10 and the intensity set at 6 the spoutHeight only reaches 6 before the coroutine moves on. I can see the problem but can’t see away of fixing it the way i have it currently set up unless i have another coroutine running the light?!

As you may have guessed i’m new to scripting so any help is greatly recieved :slight_smile:

Use a boolean or (||) instead of a boolean and(&&)- that way it will continue to do its thing until both conditions are false. I should warn you that doing this will cause your lowset value to rise above where it’s supposed to, because you are incrementing them at the same time- I would use a single ‘modifier’ value which goes between 0 and one, and then use

lavaWhatever.value = Mathf.Lerp(lowest value, highest value, modifier);

for both of them. That way, they will increase at the same rate, and won’t interfere with one another.