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 Michael 16 · May 12, 2011 at 05:43 PM · delayammoprogress-bar

How to reset health/energy/progress bars and how to show the ammo in numbers?

I asked that question before but I got no answer and I stuck so I need to solve that problem to continue making my game!!!

I make a third person game and I created in the game health, energy and delay bars based on this answer. I edited a bit the code so it will be fit to my game (bar for each weapon and time to fill each bar) and the bars are seeing all the time.

In the question I used for help, asked about how to reset the bar but I try to do wat the answed say and it don't works in my game.

Here is parts my code (the code is very long and you don't need to see all of it...):

//2D bars
 var basicSize : Vector2 = new Vector2(150,20);
 var otherSize : Vector2 = new Vector2(100,10);
 //health
 var healthBarDisplay : float = 100;
 var healthPos : Vector2 = new Vector2(130,60);
 var healthBarEmpty : Texture2D;
 var healthBarFull : Texture2D;
 //energy
 var energyBarDisplay : float = 100;
 var energyPos : Vector2 = new Vector2(130,85);
 var energyBarEmpty : Texture2D;
 var energyBarFull : Texture2D;
 //bombs
 var bombBarDisplay : float = 0;
 var bombPos : Vector2 = new Vector2(80,110);
 var bombBarEmpty : Texture2D;
 var bombBarFull : Texture2D;

there are 2 more weapon based like the bombs

energyBarDisplay += Time.time * 0.0002;
     energyBarDisplay = Mathf.Clamp01(energyBarDisplay);
     bombBarDisplay += Time.time * 0.0015;
     bombBarDisplay = Mathf.Clamp01(bombBarDisplay);
     teleBarDisplay += Time.time * 0.0015;
     teleBarDisplay = Mathf.Clamp01(teleBarDisplay);
     rocketBarDisplay += Time.time * 0.0002;
     rocketBarDisplay = Mathf.Clamp01(rocketBarDisplay);

what i did for reset:

function Explode ()
 {
     if (bombUse)
     {
         animation.Play("RockExp");
         var expEffect1 = Instantiate(bombEffect1, GameObject.Find("spawnPointA").transform.position, transform.rotation);
         yield WaitForSeconds(0.5);
         var expEffect2 = Instantiate(bombEffect2, GameObject.Find("spawnPointA").transform.position, transform.rotation);
         var ballExp = Instantiate(bombs, GameObject.Find("spawnPointA").transform.position, Quaternion.identity);
         ballExp.gameObject.tag = "CharacterProjectile";
         ballExp.rigidbody.AddForce(transform.forward * 10000);
         bombBarDisplay = 0.0f;
         //for delay
         bombUse = false;
         yield WaitForSeconds(5.0);
         bombUse = true;
     }
 }

to make the bars:

//bombs
     // draw the background:
     GUI.BeginGroup (new Rect (bombPos.x, bombPos.y, otherSize.x, otherSize.y));
     GUI.Box (Rect (0,0, otherSize.x, otherSize.y),bombBarEmpty);
     // draw the filled-in part:
     GUI.BeginGroup (new Rect (0, 0, otherSize.x * bombBarDisplay, otherSize.y));
     GUI.Box (Rect (0,0, otherSize.x, otherSize.y),bombBarFull);
     GUI.EndGroup ();
     GUI.EndGroup ();

The problem is when I use the weapon and the delay works (there is a delay) the bar needs to show on how much time I will be able to use the weapon again, When I use it the bar reset but it filled again in notime (it flashing with the empty bar). how I can reset the bars?

Another question I want to ask is how to show by numers the bullets I have, show the ammo?

Comment
Add comment
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

1 Reply

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

Answer by Antony-Blackett · May 12, 2011 at 09:50 PM

Your problem with the bar going straight back to full is that you are using Time.time. Time.time is the total time since your game started. What you want to use it Time.deltaTime. This is the length of time for this frame.

energyBarDisplay += Time.deltaTime;
energyBarDisplay = Mathf.Clamp01(energyBarDisplay);
bombBarDisplay += Time.deltaTime;
bombBarDisplay = Mathf.Clamp01(bombBarDisplay);
teleBarDisplay += Time.deltaTime;
teleBarDisplay = Mathf.Clamp01(teleBarDisplay);
rocketBarDisplay += Time.deltaTime;
rocketBarDisplay = Mathf.Clamp01(rocketBarDisplay);

By just adding Time.deltaTime to your bar it will take exactly 1 second to refill. You can make this longer or shorter by adding a scale like so.

public var energyChargeRate : float = 2;

energyBarDisplay += Time.deltaTime * energyChargeRate;

Now the energy bar will charge in half a second.

For your second question about displaying ammo. You need to add a guiText object or a textMesh object you your scene and set its text field to your ammo value.

public var ammoDisplay : TextMesh; public var ammo : int; public var ammoMax : int;

function Update() { ammoDisplay.text = ammo.ToString() + "/" + ammoMax.ToString(); }

Comment
Add comment · Show 3 · 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 Michael 16 · May 13, 2011 at 04:07 AM 0
Share

Ty, i'll try that.

avatar image Michael 16 · May 13, 2011 at 11:20 AM 0
Share

Ty so much! It works!

avatar image Antony-Blackett · May 14, 2011 at 12:11 AM 0
Share

No problem $$anonymous$$ichael.

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

reset delay bar and and ammo 0 Answers

Delay after Attack Button and Decreasing Mana Bar 1 Answer

How to implement a delay between each time my gun fires (using coroutines or otherwise)?... 2 Answers

android audio delayed 1 Answer

AI waypoints and delay?.. need help 2 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