- Home /
Maximum of a bar
I have a bar with maximum 25.0 that decreases to 0.0. And if I press the key button "D", the bar adds 10.0 If the bar is at 20.0 and I press the "D" key, it adds 10.0 so the bar have 30.0, but the maximum is 25.0
How can I fix this problem. If the bar have 20.0 of 25.0 and I press the "D" key that adds normally 10.0 it adds only 5.0 to fill the bar to 25.0 and not to 30.0, 5.0 above the maximum. Someone who can help me?
This is the script I use;
#pragma strict
var totalTime = 25.0;
var remainingTime = 25.0;
var BarBorder:Texture2D;
var BarBorderWidth:float=508;
var BarBorderHeight:float=28;
var Bar:Texture2D;
var BarWidth:float=500;
var BarHeight:float=20;
function Update() {
if (remainingTime > 0.0)
remainingTime -= Time.deltaTime;
if (remainingTime < 0.0009){
Application.LoadLevel("Sc_Intro_02");
}
if (Input.GetKeyDown("d")) {
remainingTime += 10.0;
}
}
function OnGUI(){
GUI.DrawTexture(Rect( (Screen.width - BarBorderWidth)*0.5,(Screen.height - BarBorderHeight)*0.95, BarBorderWidth, BarBorderHeight), BarBorder);
GUI.DrawTexture(Rect( (Screen.width - BarWidth)*0.5,(Screen.height - BarHeight)*0.945, BarWidth* remainingTime/totalTime, BarHeight), Bar);
}
Please help :D
Answer by AlucardJay · May 04, 2013 at 10:38 PM
Pseudocode :
if ( currentValue > maximumValue )
{
currentValue = maximumValue;
}
Check if your value has risen above the maximum value, if so then make the current value equal the maximum value.
So it should be like this?
#pragma strict
var totalTime = 25.0;
var remainingTime = 25.0;
var BarBorder:Texture2D;
var BarBorderWidth:float=508;
var BarBorderHeight:float=28;
var Bar:Texture2D;
var BarWidth:float=500;
var BarHeight:float=20;
function Update() {
if (remainingTime > 0.0)
remainingTime -= Time.deltaTime;
if (remainingTime < 0.0009){
Application.LoadLevel("Sc_Intro_02");
}
if ( currentValue > maximumValue ){
currentValue = maximumValue;
}
if (Input.Get$$anonymous$$eyDown("d")) {
remainingTime += 10.0;
}
}
function OnGUI(){
GUI.DrawTexture(Rect( (Screen.width - BarBorderWidth)*0.5,(Screen.height - BarBorderHeight)*0.95, BarBorderWidth, BarBorderHeight), BarBorder);
GUI.DrawTexture(Rect( (Screen.width - BarWidth)*0.5,(Screen.height - BarHeight)*0.945, BarWidth* remainingTime/totalTime, BarHeight), Bar);
}
I really don't have an idea :S
I have given an example, not the exact code.
When you press the D key, add to the time, then check if the time is greater than the totalTime :
if (Input.Get$$anonymous$$eyDown("d")) {
remainingTime += 10.0;
if ( remainingTime > totalTime) {
remainingTime = totalTime;
}
}
Your answer
Follow this Question
Related Questions
Can't get max value to be displayed! 1 Answer
Find the highest first value of a vector2 in a list? 2 Answers
Rigidbody2d max angular velocity 0 Answers
Maximum scene size? 2 Answers
progress bar 1 Answer