I need to convert a float of 28800 to 100-0%, I have tried a bunch of different things and have came up with everything but that.
could you guys show me the light?
I need to convert a float of 28800 to 100-0%, I have tried a bunch of different things and have came up with everything but that.
could you guys show me the light?
Well what is 28800? is that maximum? 50%?
It is the max.
float percentage = value / 288.0f;
To get a percentage, you divide, what your, in this case, say score is, buy what the maximum is and multiply that by 100.
What do i do with it??
Its not the score, its fuel. It starts at 28800 which I am pretty sure equals 8 hrs. it counts down to 0. but 28800 counting down isn’t very realistic, so I need a %.
Right so…
(33356/33356) * 100 = 100%
(15173/33356) * 100 = 50% (roughly. No calculator)
I’m not following you…
This is very basic maths. Take a look at this website for an explanation of percentages and how to calculate them:
What I did was this-
/
/
/
public static float FuelTime = 28800.0f;
public float Percentage = (28800/28800) * 100;
void Update()
{
FuelTime = Percentage;
}
/
/
/
And it seems to be working, I guess I just never tried watching 8 hrs tick down in percentages… Its slowly going down, but not sure yet.
I know percentages is simple math, but I am not a programmer…yet, its a bit different when your doing it like this if you haven’t done it before. Searching for help I ahve came across tons of lines of code that have complex math + vars and what not. If you are a programmer it may be simple…
Correction-
/
/
/
public static float MaxFuel = 28800.0f;
public static float FuelTime = 0.0f;
public float Percentage = (MaxFuel/FuelTime) * 100;
void Update()
{
void Update()
{
FuelTime = Percentage;
}
/
/
/
Why did all those others need complex mathf.lerp stuff going on??? this was what they wanted with none of that???
Thanks for the light…![]()
percentages is not programming maths just normal maths. I sympathise with you though on the maths → Programming bit as at the very beginning it can be a bit daunting but a percentage should easily be managable.
Tip: Write the calculation out on paper!
Maths: (simulate 50% fuel)
(50/100) * 100 = 50%
(128/256) * 100 = 50%
Programming:
(currentValue / maxValue) * oneHundred = myPercentage;
Where the following is true;
currentValue = fuel in the tank
maxValue = the maxmum amount of fuel the tank can hold
oneHundred = 100
myPercentage = the percentage you will display to the user.
Its not working…I waited and it isn’t going below 99.98765…about in 5 min or so.
can you post the code your currently using? Did you try to plan the workings out on paper/use a calculator and test your code?
Yeah I did, its pretty simple math now that I know that I don’t have to do a bunch of crazy stuff-
/
/
/
private float MaxFuel = 28800.0f;
private float FuelTime = 28800.0f;
public static float Percentage;
void Update()
{
Percentage = (FuelTime / MaxFuel) * 100;
}
/
/
/
There could be two issues that I can see, but since I cant see the full source and I dont know what your project contains I cant diagnose the exact issue. Tow things you might want to check:
A) Are you decreasing the FuelTime variable as fuel is consumed?
B) You likely dont want the Percentage variable to be static. If you have multiple tanks in the scene each running the same Update logic, they’ll all be writing to the same Percentage variable because its static and hence over-writing each others results.
Yes the fuelTime value is decreasing, and yeah I realize that the static logic will need to be address when I come to that bridge. I have other scripts referencing the value thats why I am doing that… I am not sure how else to do it.
OOps, fixed it. i have the percentage on the Time.deltaTime deal…thanks you made me check it. It works now…
I will have to revisit the ststic deal because I have a ton of code and a few other static values…