# limiting a time bar

I'm currently using a box and its scale as a time bar. it gets smaller as time goes but grows back when the enemies are killed. I've tried many ways to make the scale max out at 22 but couldn't get it to work. How can I set a limit on the time bar so it will never go over a certain value? Thanks.

``````var barScale : float = 22;
var barPosition : float = -2;
var TimeSpeed = .5;

function Update()
{
if ( transform.localScale.x > 0 )
{
barSc = barScale - (Time.time * TimeSpeed);
transform.localScale.x = barSc;
barPos = barPosition + ( barSc / 2 ) - ( barScale / 2 );
transform.position.x = barPos;
}
else
{
transform.localScale.x = 0;
}
}

// Time bar grows back when enemies are destroyed.
function LateUpdate()
{

if ( killEnemy.wasClicked == true )
{
barScale += 2;
barPosition += ( 2 / 2 );
killEnemy.wasClicked = false;
}
}

``````

MathF.Max(value_one_here, value_two_here)

Picks the lower of the two values. So you can put 22 in value_two_here and put your equation for increment in value_one_here and it should stop your box exceeding 22 units in size.

Or maybe... use a "while".

These things work kind of like this, but I'm no expert on syntax, wait for a guru of code to sort that out....

while barScale < 22 // go do this addition to barScale

And when it's done, ie barScale is greater than or equal to 22, it moves on... the problem with this is it's gonna update at the frame rate since... but again, I'm a retard and don't really know how to get it to increase to 22 progressively over time. But someone will be along shortly to mock my solutions and truly help you.

I've actually used mathF.Clamp this time and was able to set min and max values.

The slider now never goes over the scale of 22 but it stays as-is for a while when the enemies are clicked when the slider is near full scale. it seems like itâ€™s physically not scaling but the values are still being scaled up. Any ideas?

``````var barScale : float = 22;
var barPosition : float = -2;
var TimeSpeed = .5;

function Update()
{
barSc = barScale - (Time.time * TimeSpeed);
transform.localScale.x = Mathf.Clamp( barSc, 0, 22);
barPos = barPosition + ( (barScale - (Time.time * TimeSpeed)) / 2 ) - ( barScale / 2 );
transform.position.x = Mathf.Clamp( barPos, -13, -2);
}

// Time bar grows back when enemies are destroyed.
function LateUpdate()
{

if ( killEnemy.wasClicked == true )
{
barScale += 5;
barPosition += 2.5;
killEnemy.wasClicked = false;
}
}

``````