Basically im trying to make the image go transparent based off how much health there is left, example:
50 Health =
I literally just need someone who is good at math to make a formula.
Full Value = -0.15
Transparent Value = 0.99
(when i said full value that means the offset until its fully visible)
(when i said transparent value that means like the offset until its fully transparent)
I’m still surprised this isn’t in the math library yet, but this is a very common thing in game development you just use a map function https://rosettacode.org/wiki/Map_range
That might be caused by the way your gradient is set up. I tested it with my own gradient and it had no problem at the 50 mark.
I used this code:
local TS = game:GetService("TweenService")
local gradient : UIGradient = script.Parent.UIGradient
local function mapRange(a1, a2, b1, b2, s)
return b1 + (s-a1)*(b2-b1)/(a2-a1)
end
local function OnHealthChanged(health)
local minMap = -.15
local maxMap = 0.99
--// assuming health is x/100
local newOffsetY = math.clamp(mapRange(0, 100, minMap, maxMap, health), minMap, maxMap)
print(newOffsetY)
TS:Create(
gradient,
TweenInfo.new(.1),
{Offset = Vector2.new(gradient.Offset.X, newOffsetY)}
):Play()
end
for i = 100, 0, -1 do
OnHealthChanged(i)
task.wait(1/60)
end
Object is not rotated, the UIGradient is rotated by 90.
fixed the rest, but it still inverts itself to go upwards.
Like when i set the players health to 90, it progresses backwards.
Like its at the 90 health position but inverted.
Before change to 90 health:
Looks like the offset is working as it should (matches the working output), try messing around with the UIGradient rotation to see if that fixes it.
You may also need to change the initial offsetY to something else when rotating; if you do, make sure to update that in the minMap value inside the script.