- Home /
Smooth increasing and decreasing inside Invoke function?
I have a GUI progress bar which is increasing and decreasing in an Invoke function by using the pixelInset.width of the GUI.
In the script, I'm decreasing the bar with 10 and increasing it with 5. Because it is set inside of an Invoke function which is called every second, it is not smoothly increasing and decreasing the bar but takes tiny parts out of it. When I place the increasing and decreasing script inside of Update() function, it will run smoothly but then my script will not work anymore because I'm calling it inside of the Invoke function.
How can I make the bar runs smoothly without removing its statement from the Invoke function? Can anyone give me an example?
if(losingStamina == true && gainingStamina == false) {
currentStamina = currentStamina - 10;
//Decrease stamina bar GUI
staminaBar.pixelInset.width = staminaBar.pixelInset.width - 40;
} else if(losingStamina == false && gainingStamina == true) {
currentStamina = currentStamina + 5;
//Increase stamina bar GUI
staminaBar.pixelInset.width = staminaBar.pixelInset.width + 20;
You don't show your invoke statement here. Can't you just decrease the time between invokes (last parameter)?
Answer by fafase · Mar 23, 2013 at 10:25 AM
Place all this in a IEnumerator method and call StartCoroutine. If you need your coroutine to run infinitely that would go as such (not tested though :)):
IEnumerator HealthBar(){
if(losingStamina) {
currentStamina = currentStamina - 10*Time.deltaTime;
//Decrease stamina bar GUI
staminaBar.pixelInset.width = staminaBar.pixelInset.width - 40*Time.deltaTime;
} else {
currentStamina = currentStamina + 5*Time.deltaTime;
//Increase stamina bar GUI
staminaBar.pixelInset.width = staminaBar.pixelInset.width + 20*Time.deltaTime;
}
yield return null;
}
void Start(){
StartCoroutine(HealthBar());
}
EDIT: I modified the if statement since you check if one is true and other is false and the one is false and other is true. You might as well just check the first. I was also missing a } at the end...
Answer by sparkzbarca · Mar 23, 2013 at 09:15 AM
put it inside update and dont use the -10
do -10 per second
and
run it in fixedupdate (its nice since it's a GUI thing) then do -10 * time.FixedDeltaTime
basically instead of doing 10 every second do (assuming default of 50 fixedupdates per second)
1 every 1/10th of a second or .2 every 1/50th of a second
all good.
Mark as answered
(have to have stamina be a float) you can of course just mathf.round(stamina) for a nice clean display.
basically if you want less than 10 lost each time take out less each time just do it more often so it's smooth
MARK AS ANSWERED
also don't use hard nubmers
do sta$$anonymous$$aLossRate = 10
sta$$anonymous$$a -= sta$$anonymous$$aLossRate
never hard code in numbers you'll end up screwing yourself when you decide you want to have different people have differnt sta$$anonymous$$a loss rates and god damnit every script has to be changed 10 times and ARRRRGGH
Avoid saying $$anonymous$$AR$$anonymous$$ AS ANSWERED like if it was an order, particularly when you provide mistakes on your answer
time.FixedDeltaTime => Time.fixedDeltaTime;
which is somehow useless since you advise to use the FixedUpdate.
its not useless its being multipled by a variable it's only pointless if he uses .2 as a per 50th of a second ins$$anonymous$$d of the much more natural per second number and use time.fixedDeltaTime. It's a way to convert from seconds to fixedDeltaTime seconds. Thats hardly pointless.
$$anonymous$$y answer had no mistakes it wasnt code. I mean your not going to copy paste that in. it also lacks brackets and a class and a using and on and on and on.
If i was writing code i'd write it in code brackets. It was clearly pseudo code.
lastly caps gets it noticed. if your question is answered you are being ordered to mark it as answered. Thats not a request. You agreed in order to be a member of this community to do your best to mark answered questions answered. If it sounds like an order. It is one.
It's usually polite to wait for the questioner to actually say that you've answered their question before suggesting that they mark it as answered. In questions where there are more than one answer it's certainly not polite to the other members who are trying to help and may have a more appropriate answer than you.
And why would you do a GUI calculation inside FixedUpdate? $$anonymous$$akes no sense to me.
Your answer
Follow this Question
Related Questions
How to increase the speed of the character in OnTriggerEnter function? 1 Answer
How to match the speed of objects and the spawn speed of objects? 0 Answers
how to increase or decrease ammo ?? but depending of ammo in inventory 1 Answer
Increasing the speed for a few seconds doesn't work. 1 Answer
Character,Camera,Updates,Frame Rates,Jitter&Stutter, I need some explanations. 0 Answers