- Home /
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;
}
It would be easier to change the pivot of your bar to the left end, so you don't need to bother with scale-dependent positioning.
And how do you do that on an object created in Unity?
Hm, difficult to explain this clearly with a few sentences.
You can do that by creating an empty GameObject, and making your bar a child of that object. This new object's transform should have the same transform values as your original bar, and once you attach your bar to that new GameObject, you should reset the bar's transform.
Then you can apply a pivot such as "two world units to the left" by shifting the new GameObject accordingly (add (-2,0,0)), and shifting the bar transform to the opposite direction (set it to (2,0,0)). If your bar transform had rotation or scale, it will get more complicated, though.
He said specifically in the question that he didn't want to create an anchor object. But Wolfram is right; that's the best way to do it in Unity.
Answer by Wolfram · May 26, 2011 at 01:31 PM
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.
Answer by MasterDracoZ · May 26, 2011 at 01:35 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612085311im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Material stretches after being applied to sphere 0 Answers
How can I do a collision check with a specific Object? 3 Answers
How to move an object towords another using forces 0 Answers
Object disappears when zoom 0 Answers
GUI Follow Object On-Screen 1 Answer