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 Mikyjax · Jan 04, 2015 at 02:39 PM · timed

Progressing bar based on time

Hello, (Sorry for my english I m'not a native english speaker) This is my first question on this website, I'm working on my first game wich is a space shooter. Everything is working flawlessy but I'have an issue. For 2 days now, I'm trying to figure out how to make a simple progressing bar. So I created a sprite (green rectangle) put its pivot point on the bottom then simply use its scale to represent its state. I know it s just a stretch but it works for what I m trying to achieve, deformation isn't seen, it's just a green bar.

Now when I'am shooting, the bar is correctly showing the amunition left so it works, my only problem is when I reload my gun.

In the game, you can reload when you want OR automatically if the player is out of ammo, no matter how many ammo left it has to take 3 sec (timeToRefill) to reload completely.

Here is my Code for that function wich is called in the update of a class named "MainInterface". My issue is somewhere in the function : public void UpdateJauge(int maxVariable, int currentVariable,int lastVariable,float timeToRefill,bool boolToCheck)

 public class Jauge : MonoBehaviour 
 {
     private float percentModificater;
     private bool isClimbingup;
     public bool isReloadable;
     private float scaleAdd;
     private float barSize;
     // Use this for initialization
     void Start () 
     {
         isReloadable=false;
         isClimbingup=false;
     }
     
     // Update is called once per frame
     void Update () 
     {
     
     }
     public void UpdateJauge(int maxVariable, int currentVariable,int lastVariable,float timeToRefill,bool boolToCheck)
     {
         if(boolToCheck==true) //If player is reloading...
         {
             if(isClimbingup==false)//... If the Bar isn't aloready Growing Up ...
             {
                 barSize=1f-transform.localScale.y; // ...We calculate the size it has to grow to reach its final size
                 isClimbingup=true; // ... The bar is now growing up.
             }
             else
             {
                 scaleAdd = ((barSize/timeToRefill)+(barSize))*(Time.deltaTime); //time to fill is the reload time of the gun.
                 float newScale = scaleAdd+transform.localScale.y;
                 if(transform.localScale.y<1f) //... if the bar isn't 100% grown (Local scale of 1)
                 {
                     transform.localScale = new Vector3(1f,newScale,1f);//... then the bar grows.
                     isClimbingup=false;
                 }
             }                
         }
         else
         {
             ResizeJauge (maxVariable,currentVariable);    //.. when the player shoot the size of the bar follows.
         }
     }
     public void UpdateJauge(int maxVariable, int currentVariable)
     {
         ResizeJauge (maxVariable,currentVariable);    
         
     }
     private void ResizeJauge(int maxVariable, int currentVariable)
     {
         if(currentVariable<0)
         {
             currentVariable=0;
         }
         
         else
         {
             
         }
         transform.localScale=new Vector3(1f,(float)currentVariable/(float)maxVariable,1f);
     }
 }


Right now, it almost works but the effect is not fluid and it s like the bar starts fast and then progressively slow down to (sometimes) reach its final state.

I hope all of this make sense to you, I'm sure it s an easy fix and that my function is way to complicated to handle that growing bar that s why I call for help. Thanks a lot, Mike

Comment
Add comment · Show 2
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 Mmmpies · Jan 04, 2015 at 03:56 PM 0
Share

O$$anonymous$$ your English is pretty good (better than $$anonymous$$e as I originally wrote You're rather than your!), if you hadn't said I wouldn't have guessed other than the non English spelling of Gauge!

I'm not putting this as an answer because I'm just going out so don't have time to work through all the logic but I think what's happening is this.

Your calculation is taking the remaining distance to complete the bar dividing by the time for the bar to refill. As the bar gets bigger the remaining fill amount gets smaller but the division is by the full amount of time.

So it starts off pretty much right then, as the remaining fill gets smaller, that division makes a fraction which gets ever smaller, from a maths point of view it should never reach the end.

Ins$$anonymous$$d of dividing by the full amount of time keep track of how much time is left and divide by that.

Didn't have time to fully check your code, just going on mistakes I made in the past!

avatar image Mikyjax · Jan 05, 2015 at 07:33 AM 0
Share

Thank You for your answer $$anonymous$$mmpies... Too sad that the name of my class is wrongly spelled ;)

I tried what you said and it made a lot of sense when I read it... but when I throw it to the test it didn't work at all... It s still progressive and way too slow... I am a bit confused and totally lost in the calculation. Here is what I tried: I added a private variable to my class named "timeLeftToRefill"

 public void UpdateJauge(int maxVariable, int currentVariable,int lastVariable,float timeToRefill,bool boolToCheck)
     {
         if(boolToCheck==true) //If player is reloading...
         {
             if(isClimbingup==false)//... If the Bar isn't already Growing Up ...
             {
                 
                 timeLeftToRefill=timeToRefill;
                  // ...We calculate the size it has to grow to reach its final size
                 isClimbingup=true; // ... The bar is now growing up.
             }
             else
             {
                 barSize=1f-transform.localScale.y;
                 timeLeftToRefill-=Time.deltaTime;
                 scaleAdd = (barSize/timeLeftToRefill)*Time.deltaTime; //time to fill is the reload time of the gun.
                 float newScale = scaleAdd+transform.localScale.y;
                 if(transform.localScale.y<1f) //... if the bar isn't 100% grown (Local scale of 1)
                 {
                     transform.localScale = new Vector3(1f,newScale,1f);//... then the bar grows.
                     isClimbingup=false;
                 }
             }                
         }
         else
         {
             ResizeJauge (maxVariable,currentVariable);    //.. when the player shoot the size of the bar follows.
         }
     }


Anyway, thanks a lot for your help, $$anonymous$$ike

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by f4bo · Jan 05, 2015 at 04:25 PM

I uploaded a scene with a little component that should do what you ask - try it and let me know


reloadbar.zip (144.0 kB)
Comment
Add comment · Show 1 · 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 f4bo · Jan 06, 2015 at 02:33 PM 0
Share

there you go - if you download now the former script, you'll find that now acts as you need. PS: try to reply in the same thread without adding a new answer otherwise ppl could get confused.

avatar image
0

Answer by Mikyjax · Jan 06, 2015 at 11:17 AM

Hi,

I watched your script and it's what I m looking for except that in my game, the bar isn't always at zero when reloading happens.

At anytime, the player can press the middle mouse button and reload... so I have to take the actual size of the bar when the player is reloading and apply 3 seconds of "growing up" on that time (sorry again for my english)

So no matter the size of my gauge ;) it will take 3 seconds and when I apply your code to mine it only work when the bar is at zero...

But anyway, thanks a lot for the time you put in finding an answer for me... it s really amazing the time people can spend just to help. Thank you! I keep trying...

Mike

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Does anyone know how to activate different scripts OnTrigger? 1 Answer

Appear after set number of seconds 2 Answers

Timed while loop? 0 Answers

How to switch between scripts on trigger? 1 Answer

Can I switch scene on a specific time? 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