Hi all. I am currently trying to make a fuel gauge UI and I need to convert the value of the fuel from a % to an angle to tween the UI needle to.
For example when the fuel tank is 100% full the angle will need to be 60. When the tank is 50% full the angle will need to be 0. When the tank is at 0% the angle will need to be -60.
I need to tween the rotation of my UI from 60 to -60 but this needs to be done in proportion to the % of fuel left. If anyone knows how I can achieve this I would appreciate it.
1 Like
Here:
local percentage = 0.25 --example (keep it in decimal from 0-1, it's easier)
local min, max = -60, 60
local range = math.abs(min - max) -- it would be |-60 - 60| or 120
local degrees = min + (range * percentage) -- answer is -30
Work:
say percentage is 0.5
-60 + (120 * 0.5)
-60 + 60
0 --the right answer!
So, you get the percentage of the range, which is then added to the minimum angle (negative) to return the number of degrees that the percentage represents.
2 Likes
I was doing this in a GUI I was working on for this answer:
local fuel = value/180*100
local rotation = -60+fuel
line.Rotation = rotation
But wow, @TheCarbyneUniverse solution is sooooo eloquent! Love it.
1 Like