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 Spec Eight · Dec 02, 2014 at 09:48 PM · percentage

Two floats == Percentage

I feel like this should be a very easy task, the more I try new things and more research I do, the more confused I get.

The code below is from one script, the other script is essentially what holds my TextMesh(s). The way it's set up, CurrentMeter goes from 0% to 100%. Once it reaches 100% it jumps directly back to 0%.

What i'm trying to do is: I want to flip the percentages so it starts at 100%, goes down to 0%. When at 0% I want it to gradually climb back up to 100% at the same pace as the Time.deltaTime.

 public float meterTime = 0.0F;
 public float meterMax = 5.0F;
 
 void CheckMeter()
 {
     meterTime += Time.deltaTime;
 
     if(meterTime >= meterMax)
     {
          //Activate something by boolean
 
          meterTime = 0;
     }
 }
 
 void UpdateMeter()
 {
     CurrentMeter = (meterTime / meterMax) * 50;
 
     ModifyMeter (CurrentMeter)     //<---- linked to a HUD Script
 }


This is my very first post on here, so if my format is incorrect or sloppy i'm sorry. Id rather someone point out the issue and let me know so I won't do it again.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Dec 02, 2014 at 10:15 PM

If I've got it right (and it's late where I am!) then you're re-setting meterTime to 0.

Really meterTime is just a float so when you get to 100% meterMax (or just above) you need to change meterTime += Time.deltaTime to meterTime -= Time.deltaTime Until it gets to 0 again.

set a bool for positive or negative bool PosNeg and if true do meterTime += if false do meterTime -=

You'll also need a meterLow to handle when it reaches 0 (or just use 0)

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 Spec Eight · Dec 02, 2014 at 11:13 PM 0
Share

This worked great, another simple thing I felt like I should have thought of. I greatly appreciate the input. I used another bool to pause and restart the meterTime in the opposite direction.

avatar image
0

Answer by SquigglyFrog · Dec 02, 2014 at 10:43 PM

Another way is to just have a INT specifying which direction its going..

 int direction = 1;
 meterTime += Time.deltaTime * direction;
 

When you want to change direction, just do

 direction *= -1;
 
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 Kiwasi · Dec 02, 2014 at 10:48 PM

Some simple mathematical tricks that will help.

  • % operator. This returns the remainder after division.

  • 1 - . This can be used to flip a value.

No sure is % is appropriate for your code. But you can implement the subtraction simply by

 CurrentMeter = (1 - meterTime / meterMax) * 50; // Why 50? Percent is 100

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 Spec Eight · Dec 02, 2014 at 11:12 PM 0
Share

I feel a bit foolish, I don't know why I didn't think of using a 1 -. Thank you for that tip. The reason it is set to 50 is because when multiplied by 50 it goes from 0 - 100. If I set it to 100 it runs from 0 - 100. Doesn't make any sense to me.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

iTween Put On Path percentage 0 Answers

How do I use one variable to control the progress of two Mathf.Clamp 1 Answer

while loop & array- turn on something via percentage 1 Answer

What's the best way to create a healthbar which represents the percentage of health the player has left? 2 Answers

How can I dynamically change the percentage value so that it always adds up to 100%? 1 Answer


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