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?
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 :)):
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…
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 staminaLossRate = 10 stamina -= staminaLossRate never hard code in numbers you'll end up screwing yourself when you decide you want to have different people have differnt stamina loss rates and god damnit every script has to be changed 10 times and ARRRRGGH
Avoid saying MARK 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.
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.
i don't care what you mark as answered only that you mark it. most questioners dont reply. They take the answer and leave. Its a legitimate issue. It causes tons of open questions. I've never had anyone mark as answered if they didn't feel it was. Surely I can't imagine anyone would. If however you don't feel like replying you might just go yay take your answer and forget. Thats wrong, needs marked.
You don't show your invoke statement here. Can't you just decrease the time between invokes (last parameter)?
– robertbu