Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rich Coy · Mar 30, 2011 at 10:53 AM · timercountdown

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.

  1. Instead of counting up, what math would I use in the Update function to start full and count down?

  2. 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; }

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AngryOldMan · Mar 30, 2011 at 11:04 AM 0
Share

Ask one question per post please, makes getting a definitive answer more likely and helps with UA's archiving of question/answers

avatar image Rich Coy · Mar 30, 2011 at 11:58 AM 0
Share

Okay, sure. $$anonymous$$y thinking was I wasn't clogging up the site with multiple posts for related questions. :)

avatar image AngryOldMan · Mar 30, 2011 at 04:48 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rich Coy · Mar 30, 2011 at 06:46 PM 0
Share

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.

avatar image Jason B · Mar 30, 2011 at 06:59 PM 0
Share

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.

avatar image Jason B · Mar 30, 2011 at 07:05 PM 0
Share

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.

avatar image Rich Coy · Mar 30, 2011 at 07:55 PM 0
Share

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. ;)

avatar image
1

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);
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FLASHDENMARK · Mar 30, 2011 at 04:40 PM 0
Share

Yeah, simple mathematics. +1.

avatar image Rich Coy · Mar 30, 2011 at 05:35 PM 0
Share

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.

avatar image Rich Coy · Mar 30, 2011 at 05:46 PM 0
Share

Part of my problem might be that I don't understand what the * 0.05 is doing.

avatar image AngryOldMan · Mar 30, 2011 at 07:04 PM 0
Share

it's multiplying the Time by 0.05 try this (check updated post)

avatar image AngryOldMan · Mar 30, 2011 at 07:07 PM 0
Share

then of course add a variable to change the decrease rate (i suggest var DecreaseRate : float = 1.00)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges