Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by tommyzat · May 31, 2017 at 07:57 PM · c#mathmathfrangemaths

How do I convert a number and a range to another directly proportional number and range?

This question is really hard to formulate.

Let's say I have a range of 0 - 100, and the number 56.

I want to convert the number 56 to be directly proportional to the range of (0 - 3) and convert it in tsaid range.

Example: Number 50, range is (0 - 100) Output number is 1.5 in the range (0 - 3), directly proportional to what is above

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by SammyDee · May 31, 2017 at 09:26 PM

As long as 0 is the first number in both your ranges, you would simply divide the number in the first range by the upper limit of the first range and then multiply by the upper limit of your second range.

In your example: 56 / 100 * 3 = 1.68 So 56 in (0 - 100) would be equivalent to 1.68 in (0 - 3)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
5

Answer by elenzil · Jun 01, 2017 at 06:56 PM

I would suggest using InverseLerp and Lerp.

 float normalizedValue = Mathf.InverseLerp(range1Min, range1Max, value1);
 float result = Mathf.Lerp(range2Min, range2Max, normalizedValue)

you could also combine those into a single statement if you wanted.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Max_Bol · Aug 15, 2018 at 11:29 PM

The universal way of handling this is actually quite simple, but it requires you to see the whole thing from a different perspective.

.

Here's the formula: (V × R2 ÷ R1) + (M2 - M1)

. V = the value you want to convert. R1 and R2 are both differential values of each ranges (maximum value - minimum value). M1 and M2 are both minimal values of each range.

.

For the example given, this gets (56 × 3 ÷ 100) + (0 - 0) = 1.68f.

.

Let's use the same formula to convert 56 in a range of 0 and 100 which could be a volume slider for your game into a range of -80 and 0 which is in dB (sound volume units used by Unity). (56 × 80 ÷ 100) + (-80 - 0).

.

As you can see, the differential value of the volume in dB is 80 because there 80 floating units between -80 and 0. The result to that means that a volume of 56% is at -35.2dB.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elenzil · Aug 16, 2018 at 05:25 AM 1
Share

this is not correct.

consider this example:

 range1 = [1, 3]
 range2 = [10, 30]
 vIn = 2 
 
 the input is halfway through range1, so we would expect the output to be halfway through range2: 20.
 
 vOut = (V * R2 / R1) + ($$anonymous$$2 - $$anonymous$$1)
 vOut = (2 * (30 - 10) / (3 - 1)) + (10 - 1)
 vOut = (2 * 20 / 2) + 9
 vOut = 29
 
 the correct general formula is:
 (V - $$anonymous$$1) * R2 / R1 + $$anonymous$$2
 
 vOut = (V - $$anonymous$$1) * R2 / R1 + $$anonymous$$2
 vOut = (2 - 1) * (30 - 10) / (3 - 1) + 10
 vOut = 1 * 20 / 2 + 10
 vOut = 20

avatar image Max_Bol elenzil · Aug 16, 2018 at 09:37 AM 0
Share

You are right... I'm sorry for misleading with my erronous formula. Still, I couldn't get your formula to give the right value in converting 56% (vIn 56 on [0,100] range) into [-80,0] range. Could you elaborate with that example? (56-0) * (100-0) / (0 + 80) - 80 = -10 while it should be between -30 and -40 (-40 being 50%).

But, I'll write it to my defense... while my formula is wrong, for some strange reason, it works 100% of the times I needed it. It's used over 10 times and, each time, it gives out the exact value that I need. I'm using it to manage volumes, converting mouse's position into a different kind of float range, etc.

=== A $$anonymous$$O$$anonymous$$ENT LATER ===

Important note, what's following isn't really your typical maths. It's something that can be quite puzzling for anyone who believes that numbers can only be a straight and ruled thing.

This really puzzled me for a couple of hours and I have found out "why" it works in my circumstances and not in some others. Funny as it might sounds, it all came back an argument I had with a $$anonymous$$ath $$anonymous$$cher at High school. Basically, I made him realize that there are 2 kinds of math: Logical and Physical.

The reason why my formula worked for me is because it's not wrong if you consider something like an Integer value. As integer value can't be divided into two identical values if it's an odd number, it gets easily wonky when you try to divide the integer 3 by 2. (If you divide an integer of 3 by 2, you get 2 as a result as it's impossible to result with 1.5 with integers.) It seems that all the cases in which I used my formula were all cases I call "bubble cases".

Now, what happens if you play with both integer (which can't be divided in usage) and floats? Yeah, it's a mess! In my cases, the formula worked because I'm converting a value within a certain range of masses and because that value represent a mass by itself.

 In the example of [1,3] in range with value of 2 converted to [10,30], in terms of space and masses, 2 becomes a mass that isn't a single unit anymore.
 
 Each of those has a mass of 1 unit each.
 |1|2|3|
 
 If you extend it to |10| [...] |30|, that means you got 21 units involved afterward.
 |10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|
 
 Notice how it's not 20, but 21 units? That's similar to the concept where Lenght() or Count() always return 1 number above the last "id" available in an Array[ ] or a List< >. That's because the length/count doesn't includes 0 in the units counted while the Array[ ] and List< > do have a instance of 0.
 If we return back to the list of 10 to 30 units.
 |1|**2**|3|
 
 becomes...
 
 
 |10|11|12|13|14|15|16|**17**|**18**|**19**|**20**|**21**|**22**|**23**|24|25|26|27|28|29|30|
 
 What's in **bold** is what has become of 2 when stretched. Each of the 3 units became 7 units each. So, the "value" that was at the end of 2 in [1,3] is now at the end of 23 in [10,30]. (2*21/3)+(10-1) = 23.
avatar image elenzil Max_Bol · Aug 16, 2018 at 04:14 PM 0
Share

heya - yeah, i noticed that your math worked for the two values i checked it against as well, which were 0 and 100. that surprised me.

I couldn't get your formula to give the right value in converting 56% (vIn 56 on [0,100] range) into [-80,0] range. Could you elaborate with that example? (56-0) * (100-0) / (0 + 80) - 80 = -10 while it should be between -30 and -40 (-40 being 50%).

you multiplied by R1 and divided by R2, ins$$anonymous$$d of the reverse.

 range1 = [  0, 100]
 range2 = [-80,   0]
 r1 = 100
 r2 =  80
 v1 =  56
 
 v2 = (v1 - m1) * r2 / r1  + m2
 v2 = (56 -  0) * 80 / 100 + -80
 v2 = -35.2
Show more comments
avatar image
0

Answer by webertbrito · Apr 03, 2020 at 05:19 PM

Hi, it's been posted a while now but I have an issue with the same nature but the formula does not apply. What if the initial or final range is reverse? For exemple:

Initial range: [237,12] Final range: [14,114]

How do I convert a number from initial range to final range?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elenzil · Apr 03, 2020 at 05:40 PM 0
Share

can you describe the output you want for an example input ? eg, if the input is 237, do you want 14 or 114 ?

If you go the route of inverselerp() / lerp(), you can reverse the direction of the interpolation by subtracting the normalized value from 1. eg

  float normalizedValue = $$anonymous$$athf.InverseLerp(range1$$anonymous$$in, range1$$anonymous$$ax, value1);
  float flippedNormalizedValue = 1.0f - normalizedValue;
  float result = $$anonymous$$athf.Lerp(range2$$anonymous$$in, range2$$anonymous$$ax, flippedNormalizedValue )

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

364 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Predict the hit position 0 Answers

How to get an object to turn to point to a direction using the shortest path. E.g. 15° to 345° in 30° instead of 330°. 0 Answers

Save object position after drag? 0 Answers

how do i Limit camera rotation on Y-Axis 1 Answer

Get angle of line based on worldspace 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges