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 Flisijn · Mar 23, 2013 at 01:06 AM · updatesmoothinvokeincreasedecrease

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;
Comment
Add comment · Show 1
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 robertbu · Mar 23, 2013 at 06:11 AM 0
Share

You don't show your invoke statement here. Can't you just decrease the time between invokes (last parameter)?

2 Replies

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

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...

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
avatar image
0

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

Comment
Add comment · Show 10 · 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 sparkzbarca · Mar 23, 2013 at 09:17 AM 0
Share

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

avatar image fafase · Mar 23, 2013 at 10:29 AM 2
Share

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.

avatar image sparkzbarca · Mar 23, 2013 at 11:10 AM 0
Share

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.

avatar image whydoidoit · Mar 23, 2013 at 11:13 AM 1
Share

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.

avatar image whydoidoit · Mar 23, 2013 at 11:15 AM 1
Share

And why would you do a GUI calculation inside FixedUpdate? $$anonymous$$akes no sense to me.

Show more comments

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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