# Scaling a UI bar for progress in Unity

I have created a progress bar inside Unity for one of my energy systems. The bar progression works fine. Its just a plane pivoted on one of the faces so scaling it in x axis makes it progress further as seen in the image below Now I am trying to implement a feature where when the bar stops progressing I would want to show a red bar indicating that it has stopped like in the image below

To implement this I would need to know somehow that the bar has stopped scaling and not progressing any further so that I can put the red bar at the position where it stops scaling.

But how do I know when the yellow progress bar stops progressing as its position stays the same throughout and its just scaling in the x direction.

• why not translate based on the progression – ratchet freak Nov 15 '14 at 0:49
• Could you please be more specific as to how do I make a solid bar progress with just translation? – ckzilla Nov 15 '14 at 1:35
• By translate, think he's referring to translating the scale of the object. – Mike Hunt Jan 14 '16 at 12:53
• You're looking for: size_of_object_in_units * current_scale / default_scale – Draco18s Jun 29 '17 at 20:12

Take a look at Renderer Bounds

Which gives you a world space AABB that you can use to determine the scale along the axis.

Now if you have a rotated loading bar you can just use the transform's lossyscale to do a similar thing but the rotation will make it slightly inaccurate.

Once you know the scale along an axis you can use an origin point to define an offset from the origin point where your red bar should appear.

I have included this poorly drawn image to help.

If I understand your problem correctly, it is as follows:

Problem: You have no way of knowing when your progress bar stops scaling/progressing in x value. You would like to position a red bar (for player feedback) at the end of the progress bar only if it stops scaling/progressing.

If this is correct, I think that I may be able to help.

Answer: It seems as though you are in need of a "status bool". Status bools are very important when you are trying to record and communicate binary states (Ex. on/off) to other scripts. I am not sure if you have a separate script that controls the function of the red bar, but the method should be similar:

private bool isScaling = false; // Assuming that the bar defaults with no scaling.

void Update () { // Update function is used in this example for testing purposes only.
if (isScaling) {
// The red bar is not visible.
} else {
// The red bar is visible.
}
};


That line of thinking will work for knowing when to show or hide the red bar. The other problem that you will face is actually setting the 'isScaling' bool to the correct value at the appropriate times. For that, you will probably need to create (or modify) a function that sets 'isScaling' to 'true' when you are scaling the bar and to false when you are not. I hope this helps!

• The problem is not finding out if the bar is scaling or not. The problem is finding the position of the transform point when the progress or the scaling stops in the x direction. I am not translating the bar but just scaling it in x direction so there is no way for me to find its position when its scaled. – ckzilla Nov 15 '14 at 23:21
• @ckzilla if you parent your red bard with the correct offset to the plane it should move with it. Then when the scaling stoppes just make it visible. – Uri Popov May 16 '16 at 10:21

As Jackoftraes said you want to have a bool for displaying if the bar should be displayed.

So you don't know if it moves by the position but you do know if the scale changes. So every frame check if the scale is the same as last frame (or the past few frames), then once the scale has been the same for enough frames show the red bar.

Hope this helps :) (even though i'm late to the party)

You could use a slider, if you need more option for the sliders then 0.x to 1.x then you can set the max and min value to whatever you like. You can set a bool on this and say if value in changing bool is true, and make an IEnumerator wait x seconds, and then show the slider handle after that time.