Problem calculating time progress in bar

Hi,

I’m trying to do a progress bar of the level, but it depends on time, and not on location (i.e. the level lasts X seconds). I’ve searched around for an answer but came out empty. (almost everything else is location related)

the visual is via 2 cubes, as one is the progression and the other is total length and the first is the child of the latter. as a result it needs to move from -0.5 to 0.5 in X seconds.

I tried to calculate this, but I’m missing a key factor (multiplier) that I can’t figure out :frowning:
The key factor isn’t fixed, because when i tried various times, it changed…

I tried this: [C#]

public GameObject progBar; //the child progress bar
public int LevelTime = 10;

Vector3 progVector; // a temp var for vector changes
float deltaSegment;
float timer;

void Start () 
{
	timer = 0;
	deltaSegment = LevelTime / Time.deltaTime; //to figure how much it needs to move each frame
}

void FixedUpdate()
{
	gameProgress();
}

void gameProgress()
{
	timer += Time.deltaTime;
	progVector = progBar.transform.position;
	progVector.y += (1/deltaSegment) * ???; // THE SILVER LINE
	progBar.transform.position = progVector;
	if(progBar.transform.localPosition.y >= 0.5f)
		Debug.Log ("Reached the end (" + progBar.transform.localPosition.y + ") at " + timer + " Seconds"); //debugging how long it takes it to reach the end
	if(timer >= LevelTime)
		Debug.Log ("Level finished");
}

I’ve tried calling through Update and FixedUpdate, but no change there. and if I try through GUI, I think I’ll have the same problem…

any suggestions, please?

Normalise the number.

  1. if the target progress bar size is 10 units wide
  2. if the source maximum progress bar size is 30 units wide

new size = ( current size / maximum size ) * target max size

Then it will fit in.

If it’s already a number between 0 and 1 then simply multiply by progress bar maximum size.

it’s that simple, and it worked. :slight_smile:

thanks!