Stretch a bar to the right?

I want to stretch a bar to the right only, while keeping it anchored in place on the left. I do not want to have to make an invisible object to “anchor” it, nor do I wish to use GUI at this time. Here’s my code so far…

static var currParanoia = 99;
static var maxParanoia = 100;

private var paranoiaBarInitialScale: float;
private var paranoiaBarLastScale: float;


function Start()
{
	transform.renderer.material.color = Color.yellow;
	transform.localScale.x= transform.localScale.x*currParanoia/maxParanoia;
	paranoiaBarInitialScale = transform.localScale.x*currParanoia/maxParanoia;
}

function Update()
{
	// Ensure paranoia can never drop below current fear level
	if (currParanoia > FearBarScript.currFear) 
	{ 
		// Reduce Paranoia by 1% every 10 seconds
		var timeDecrease = Time.deltaTime * -0.01;
		ApplyParanoia(timeDecrease);
	}
}

function ApplyParanoia (paranoia : float)
{
	// if you are already at max paranoia, do nothing
	if (currParanoia == maxParanoia) return;
	
	// increase current paranoia by amount passed by the ability triggered
	currParanoia += paranoia;
	
	// keep paranoia bar start point unchanged
	paranoiaBarLastScale = transform.localScale.x;
	
	// stretch the paranoia bar fill by a proportional amount to the max paranoia
	transform.localScale.x = paranoiaBarInitialScale*currParanoia/maxParanoia;
	
	//ensure paranoia bar start point is unchanged 
	transform.position.x -= (paranoiaBarLastScale - transform.localScale.x)/2;
}

A pivot/anchor would be the most elegant and straight-forward way to do it.

If you really insist on doing it mathematically via the transform: there are several problems and unnecessary complications with your current script.

For starters, the condition “if (currParanoia == maxParanoia)” will (almost) never be met. Instead, first add currParanoia += paranoia; and then clamp to maxParanoia afterwards.

Secondly, your paranoiaBarInitialScale alrady contains the ratio curr:max, so you mustn’t apply it again in ApplyParanoia(). There are several ways to solve this. What you need is the ratio of change oldParanoia:newParanoia. For example, extending your approach, you could store initialParanoia=currParanoia; in Start(), and then use

transform.localScale.x = paranoiaBarInitialScale*currParanoia/initialParanoia;

Finally, your approach to shifting the position will only work if your bar’s x-dimension is exactly 1 world unit. Also, you’ll need to += your amount, not -=

On the other hand, about 3.5 gazillion people already must have been using progress bars in the past, even without OnGUI() or GUITextures, so I’d suggest searching answers/forum/wiki for example-scripts for that (I’m almost certain in one of Unity’s examples/tutorials on their website will already be a script for such a progress bar), instead of trying to invent the wheel for the n’th time.

An entirely different approach would be to use a cutout shader. Search Unity Answers for “circular progressbar” for that.

Ended up having to go with the empty object to get it to work, and the current paranoia can eventually equal max paranoia as the plan is to pass effects that increase it.