public class RaceProgressBar2 : MonoBehaviour {
// set GUI bar width and height in the Inspector
// private float barWidth = 350;
// private float barHeight = 25;
public UISprite bar;
// drag a texture as the icon to move on the progress bar
public UISprite progIcon;
// where to set the GUI element to
private float barProgress;
// empty objects represent the start and end of a level
public Transform startPoint;
public Transform endPoint;
// current Player position
private Transform playerPos;
void Update ()
{
if (playerPos == null)
{
playerPos = GameObject.FindGameObjectWithTag("Player").transform;
}
// get level distance by subtracting start and end
float totalDist = endPoint.position.x - startPoint.position.x;
// get player distance from start in X axis only so slopes / height doesn't affect result
float playerDist = playerPos.position.x - startPoint.position.x;
//get player's progress as a percentage of the whole distance
float playerProgress = playerDist / totalDist * 100;
//turn the playerProgress percentage back into the scale of barWidth
barProgress = playerProgress / 100 * (1-bar.gameObject.transform.position.x); // playerProgress / 100 * barWidth;
progIcon
.transform.position = new Vector3
(barProgress,
0,
0); }
void OnGUI ()
{
// create a GUI group the width of the bar and twice its height
// in order to leave room for 'Start' and 'End' text under the bar
// GUI.BeginGroup (new Rect (10, 10, barWidth, barHeight*2));
//draw a box as the backing for the progress bar, blank text inside
// GUI.Box( new Rect(0,0,barWidth,barHeight),"");
// create a label to draw the progress icon texture, use barProgress var
// to set its X position, 0 as the Y position and width and height of the texture used
// GUI.Label ( new Rect(barProgress, 0, progIcon.width, progIcon.height),progIcon);
// add start and end labels
// GUI.Label( new Rect(progIcon.width/2, 25, 50, barHeight),"Start");
// GUI.Label( new Rect(barWidth-30, 25, 100, barHeight),"End");
// GUI.EndGroup();
}
}