How Do I Get A Percentage Between Two Numbers From Two Other Numbers?

I am currently working on a Throttle System for an Aircraft, and I use a NumberValue between 0-100 to track how much throttle there currently is in the engine. My goal is to set up multiple percentage-based systems based on the 0-100 NumberValue, but im having difficulty with the math.

For example: The speed of the Aircraft in LinearVelocity is a Maximum of 100, and Minimum of 40.
When the Throttle is at 0, the speed should be 40 since that is the minimum speed allowed while airborne. When Throttle is at 100, the speed should be 100 since that’s the max. And finally a Throttle of 50 would be 70 in speed, since that’s the halfway point between 40 and 100. Another example would be the Engine Sound, which has a set PlaybackSpeed based on the Amount of Throttle. 0.9 Is the minimum, 1.3 is the maximum, it links up to the 0-100 percent from the Throttle, etc etc, you get the idea.

Ive scowered the entire DevForum and couldn’t find anything relating to my exact situation, the closest thing I could find is a post about getting the percentage between two numbers, but that’s more for telling the player how far they’ve gotten in a level, or for GUI. I tried it anyways and as expected, the numbers were always inaccurate on at least one side of the spectrum.

Im trying to set a percentage between two numbers, based on the percentage of two other numbers, With a total of four numbers involved. (Two pairs of min/max’s). Im starting to wonder if its even possible.

I drew up a demonstration incase anyone is confused, ive never been the best explainer.


Basically, all minimums and maximums must line up with the Throttle and its min/max.

Ive been stuck on this for hours, any help would be greatly appreciated!

Sounds like you need a linear interpolation function

local function lerp(min, max, percent)
	return min + (max - min) * percentage
end

This returns a number a certain percentage of the way between the two given numbers.

2 Likes

Quick correction, you defined the third parameter as “percent” but you’re multiplying the number from [min + (max - min)] by “percentage”.

Parameter 3 defined as “percent”

Here it’s being defined as “percentage”

Other than that, the linear interpolation (lerp) should work just fine :slight_smile:

1 Like

I think a lerp function won’t be enough, really. I think you need a numerical mapping function.

local function map(x, in_min, in_max, out_min, out_max)
    return out_min + (x - in_min)*(out_max - out_min)/(in_max - in_min)
end

This looks a bit complex, but fear not:

  • x is the value you want to convert.
  • in_min and in_max are the first range you want to map to. Say, your throttle, being 0 and 100, for example.
  • out_min and out_max are the output range that x will be mapped to.

This is likely what you want. I hope this helps you.

1 Like

@fireys is right, you just need to divide percentage by 100 at the end to get the correct number.

1 Like

I tested this, and it worked perfectly for all systems involved! Thank you so much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.