- Home /
Timer Bar Modifications
Hello, I'm using the following script and it works fine but I'm lost as to how to change two things.
Instead of counting up, what math would I use in the Update function to start full and count down?
I have pick ups in my levels that add to the player's time. How would I target this script and add 25 seconds back on to the countdown? Currently in my collision.js when a collision occurs with an object tagged "Battery" I add 25 to a variable named PLAYER_LIFE.
Also, is this method processor intensive? Eventually I'd like to release on the iPhone.
Thanks for any help.
var barDisplay : float = 0; var pos : Vector2 = new Vector2(20,40); var size : Vector2 = new Vector2(200,20); var progressBarEmpty : Texture2D; var progressBarFull : Texture2D; var fullVariable : float = 200; var decreaseRate : float = 1.00;
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() { barDisplay = fullVariable - Time.time * decreaseRate; }
Ask one question per post please, makes getting a definitive answer more likely and helps with UA's archiving of question/answers
Okay, sure. $$anonymous$$y thinking was I wasn't clogging up the site with multiple posts for related questions. :)
ah i see what you mean, your life counts down hence why you need "batteries" to recharge. Unfortunatly they are two different question, even though they do relate to the same thing on your game.
Answer by Jason B · Mar 30, 2011 at 06:20 PM
Multiplying things by a decimal is a good way of saying percentage. If I multiply 100 0.95, I'll get 95% of 100, or 95. I commonly do this to get a percentage of screen space on different resolutions. Screen.width 0.5 = 50% of the screen width.
Anyways... Except rather than what Bobadebob did, I would just do:
FullVariable -= 1.0 * Time.deltaTime;
This means FullVariable will go down by 1 per second. (Any time you multiply a value by Time.deltaTime, you can expect it to mean that whatever you're changing will change by that amount per second, if it's inside of Update).
Thanks for your help. I've modified my initial code above to include your suggestions but the bar still sits at full without counting down.
Pretty sure you need to apply this resizing to the actual GUI.Box of the bar itself, not the group it's in. I've actually never used GUI.Group before... I'll have to check that out in the manual. In the meantime, apply your math to the X value of the filled in GUI.Box rather than the group.
Right, so I've just read about groups. You don't need to be making groups for either of those boxes. Groups are specifically for creating collection of new GUI objects that use the group's boundaries as new coordinates. Personally, I would remove your grouping altogether. Regardless, I'm almost 100% sure now that the problem is you're resizing the group and not the actual GUI Box.
I think I understand. I've only been coding in Unity for less than a week. Any example of what you are describing you can point me too? This code was from a tutorial, not something I coded myself. ;)
Answer by AngryOldMan · Mar 30, 2011 at 11:03 AM
depends what "full" is in your update function.
barDisplay = FullVariable - Time.time * DecreaseRate;
It's simple mathmatics, subtract and add. Been caught up with something like this myself, kept trying more complex solutions when the answer was so simple. For accessing variables in different scripts you either make them static variables or use GetComponent
and for the final edit your problem is this
GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
dont resize the group resize the box the texture is in.
GUI.BeginGroup (new Rect (0, 0, size.x, size.y));
GUI.Box (Rect (0,0, size.x * barDisplay, size.y),progressBarFull,ScaleMode.StretchToFill);
Thanks. Okay, I created a variable named "FullVariable" and set it to 200 at the top of this script. I then tried to use what you showed above and the bar indeed starts at full but does not go down over time.
Part of my problem might be that I don't understand what the * 0.05 is doing.
it's multiplying the Time by 0.05 try this (check updated post)
then of course add a variable to change the decrease rate (i suggest var DecreaseRate : float = 1.00)
Your answer
Follow this Question
Related Questions
Countdown Timer Help About putting 0's 1 Answer
Timer counts down unwanted 2 Answers
How to stop a timer with GUI button? 1 Answer
Countdown timer in minutes:seconds:milliseconds 2 Answers
Timer not work :( 0 Answers