distance bar

Hey, im trying to make a bar which will show how far are you from the end im not sure how to do it… kinda stuck.
i got how to see the distance bitween 2 objects and how to make the progress bar but to make them fit each other is where i got stuck :?

var other : Transform;
var barDisplay : float = 0;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;

function OnGUI()
{

    // draw the background:
    GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
        GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);

        // draw the filled-in part:
        GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
            GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
        GUI.EndGroup ();

    GUI.EndGroup ();

} 

function Update()
{
    var dist = Vector3.Distance(other.position, transform.position);
    print ("Distance to other: " + dist);
    // for this example, the bar display is linked to the current time,
    // however you would set this value based on your desired display
    // eg, the loading progress, the player's health, or whatever.

barDisplay = dist;
}

and yeah i know the last line is not good thats what im trying to fix tho.

if the world is linear (aka you can start at 0 and work to 100% of the level) then you want to take the % of the distance that is done, and fill the bar that way

aka

barDisplay = dist/totalDist;

thanks for the replay but i get an error that it doesnt know what is total dist, should it be a variable?
sorry cause im bad at scripting… :cry:

you need to calculate the total distance for someone to travel the level - aka the distance between the starting point and the ending point

then you save that variable as totalDistance

then by taking dist/totalDistance - you get the % of the level that you have completed

after that when you times that % by the x value in your GUI element, you will get a bar that fills based on % done with the level

sorry for being annoying.
i dont really know how to do that, im just a beginner.
the scene is in side scrolling so you can go both x and -x and jump.
so if i make 2 gameObjects 1 for start and one for the ending, how i will make the distance between thos into percent?
thanks.

i think this should do it

var other : Transform; 
var barDisplay : float = 0; 
var pos : Vector2 = new Vector2(20,40); 
var size : Vector2 = new Vector2(60,20); 
var progressBarEmpty : Texture2D; 
var progressBarFull : Texture2D; 
var totalDistance : float = 0;

var startPoint : GameObject;
var endPoint : GameObject;

var Start(){
totalDistance = Math.Abs(endPoint.transform.position.x - startPoint.transform.position.x);
}

function OnGUI() 
{ 

    // draw the background: 
    GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y)); 
        GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty); 

        // draw the filled-in part: 
        GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y)); 
            GUI.Box (Rect (0,0, size.x, size.y),progressBarFull); 
        GUI.EndGroup (); 

    GUI.EndGroup (); 

} 

function Update() 
{ 
    var dist = Vector3.Distance(other.position, transform.position); 
    print ("Distance to other: " + dist); 
    // for this example, the bar display is linked to the current time, 
    // however you would set this value based on your desired display 
    // eg, the loading progress, the player's health, or whatever. 

barDisplay = dist/totalDistance; 
}

your awesome for these replays but unfortuantly it doesnt works right :cry:
its backwards and it starts from 1.6 even tho im in the same place as the start point.
but thanks for leading me closer you rock!

its backwards because you have

var dist = Vector3.Distance(other.position, transform.position);

and you want

var dist = Vector3.Distance(transform.position, other.position);

also - is other the end point??? if so you can change this line to

totalDistance = Math.Abs(other.position.x - startPoint.transform.position.x);

and one more change :

barDisplay = (totalDistance - dist)/totalDistance;

Well i have fixed it now but it goes from 100% to 0% is there any way to make it opposite?

        GUI.BeginGroup (new Rect (0, 0, size.x * (1-barDisplay), size.y));

thanks works now :wink: