Scaling Platform script

I’m trying to make a platform that scales between multiple sizes, at varying speeds and varying wait times before proceeding to scale to the next size however I haven’t been able to get it right without coding errors. Please help.

var ScaleSize:Vector3[];
var ScaleSpeed:float[];
var WaitTime:float[];
var loop : boolean = true;
private var currentScale:int;
function Update()
{
	if(currentScale < ScaleSize.length)
	{
		
	}
}

I hope this helps

var ScaleSize:Vector3[];
var ScaleSpeed:float[];
var WaitTime:float[];
var loop : boolean = true;
private var currentScale:int;
function Update()
{
	if(currentScale < ScaleSize.length)
	{
		transform.localScale.x
                transform.localScale.y
                transform.localScale.z
	}
}

it didn’t do anything. I changed it to the line below which worked but I need it to scale at a specified speed.
transform.localScale=ScaleSize[currentScale];

If you are going with different scale times and such over time, your best bet is to use Coroutines. You can control time easier than in Update

function Start(){
	StartCoroutine(ScaleMe());
}

function ScaleMe(){
	while(true){
		var pauseTime=Random.value * 3;
		var scaleTime=Random.value * 3;
		var scaleAmount=Random.Range(0.5, 3.0);
		var pauseTil=Time.time + pauseTime;
		while(Time.time<pauseTil)
			yield;
		var scaleStart=Time.time;
		var scaleTil=Time.time + scaleTime;
		while(Time.time < scaleTil){
			transform.localScale=Vector3.Lerp(transform.localScale,
				Vector3.one * scaleAmount,
				(Time.time - scaleStart)/(scaleTil-scaleStart));
			yield;
		}
	}
}

I’m currently finding it difficult to control now. I’m unfamiliar with coroutines. I can’t get it to go from one scale size to the next that I set in the array.

Well… its designed to not be controllable.

A Coroutine is a function that runs in the background over several frames. In the case of this one, it runs continuously until the program ends.

You should be most interested in this part:

		while(Time.time<pauseTil)
			yield;
		var scaleStart=Time.time;
		var scaleTil=Time.time + scaleTime;
		while(Time.time < scaleTil){
			transform.localScale=Vector3.Lerp(transform.localScale,
				Vector3.one * scaleAmount,
				(Time.time - scaleStart)/(scaleTil-scaleStart));
			yield;
		}

This basically says:

“Yield 1 frame until the current time is greater than the time it is supposed to stop.”
“The do something.”
“Yield 1 frame, adjust the size until you reach the new size”

The Lerp and scale are all part of smoothly swapping between sizes.

So if you want to say, wait 5 seconds adjust the scale over 1, this script would be ideal.

What I mean was that I haven’t been able to script in anything like if the localScale reaches ScaleSize[currentScale] then currentScale++. That’s why I had the WaitTime and ScaleSpeed variables arrays so I could just put in [CurrentScale] and then put an else statement to reset it to 0 to have it loop. As of now, it just randomly scales between varying sizes.

OK, what is WaitTime? is this the amount of time it takes to scale from one scale to another, or some timeout it takes to reject the current scaling and move onto another?

Psuedo code:

While(true){

startScale=transform.localScale;
startTime=Time.time;
waitTil=Time.time + WaitTime[CurrentScale];
While(Time.time < waitTil){
transform.localScale=Vector3.Lerp(startScale, ScaleSize[CurrentScale], (Time.time- startTime)/(waitTil - startTime));
yield;
}
CurrentScale=(CurrentScale + 1) % ScaleSize.Length;

}

Yes, WaitTime is the timeout it takes before proceeding to the next scaleSize while scaleSpeed is how fast it scales. I didn’t want it to immediately scale between different sizes but instead scale to one size, wait a little bit (like 5 seconds), scale to the next size at a different speed and then wait maybe 10 seconds. Sorry if I didn’t make that clear enough at the start.

So, is speed the seconds it takes from one scale to another?
Or is it just a factor which denotes the speed at which it scales?

More Pseudo Code

While(true){

startScale=transform.localScale;
startTime=Time.time;
waitTil=Time.time + WaitTime[CurrentScale];
While(Time.time < waitTil){
transform.localScale=Vector3.Lerp(startScale, ScaleSize[CurrentScale],[COLOR="red"] ScaleSpeed[CurrentScale] * Time.deltaTime[/COLOR]);
yield;
}
CurrentScale=(CurrentScale + 1) % ScaleSize.Length;

}

Speed is the seconds it takes to scale from one size to the next.

right now it’s not working. below is the code I have.

While(true)
	{
		While(Time.time < Time.time + WaitTime[CurrentScale])
		{
			transform.localScale=Vector3.Lerp(transform.localScale, ScaleSize[CurrentScale], ScaleSpeed[CurrentScale] * Time.deltaTime);
			yield;
		}
		CurrentScale=(CurrentScale + 1) % ScaleSize.Length;
	}