- Home /
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
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!
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
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
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.
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
Your answer
Follow this Question
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