Tricky Maths Equation

I am trying to make a FOV slider with boundaries from 45 to 95 but there is a problem currently while trying to update the slider to a default value (being 80).

(Variables used in the code)

``````Min = 45
Max = 95
``````

Here is my code (The issue is in the CalculateScale function):

Results:

0.2796 is much lower than it should be

somehow it calculates 58 as opposed to 80, obviously there is a fault in my maths but I don’t know what it is.

Here’s a diagram if you need it

Thanks

2 Likes

I forgot to Include the `CalculateValue` function,
CalculateValue works fine but I’ll include it here

You can use this function to map your values.

``````function Map(n, inMin, inMax, outMin, outMax)
return (outMin + ((outMax - outMin) * ((n - inMin) / (inMax - inMin))))
end

print(Map(2, 1, 2, 45, 95))
``````

This example maps your min (45) and max (95) between the numbers 1 and 2.
Input 2 will give your max value.

You can edit the values as you want.

1 Like

If you want someone’s help don’t try and make their life harder, take every screenshot of a code and paste it as an actual code with this . This way we can copy-paste it to the studio and debug it.

1 Like

What do you mean? How am I making it harder for you?

If we want to place your code in a test project and debug it, we will have to copy it from an image which is not as convenient as taking it from a code section.
For example:
Copying this to my project

Is not as easy as copying this

``````local function CalculateValue(scale)
local v = Min + ((Max - Min) * clamp(scale, 0, 1))

return floor(v)
end
``````

Btw, just like you did at the beginning with

``````Min = 45
Max = 95
``````

It also looks way better than a bunch of pictures with different sizes [=

Fair enough, sorry about that.

1 Like
``````local minValue = 45
local maxValue = 95
local delta = maxValue - minValue

local function ValueToScale(value)
local scale = (value - minValue) / delta
return math.clamp(scale, 0, 1)
end

local function ScaleToValue(scale)
local value = scale * delta + minValue
return math.clamp(value, minValue, maxValue)
end
``````
2 Likes

Wow! This works, thank you so much for you reply!

Didn’t mean to sound negative, keep up the good work and keep asking questions [=