Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
2
Question by heaversm · Jun 27, 2012 at 07:13 PM · mathclampnormalize

how to normalize a value to a range between 0 and 1

Hi - I'm doing an audio experiment where recording the sound coming out of my speakers gives me a number between around 0 and 20. I want to convert this number to something between 0 and 1, so that I can translate the volume level gets translated to the scale of an object. How do I do this? My basic setup is:

 var audioVal:float = audio.GetOutPutData(...); //gives the audio in decibels
 var scaleVal:float; //this value will give a scale between 0 and 1;
 scaleVal = Mathf.Clamp(audioVal,0,1); //this doesn't seem to work. The value always ends up being at the maximum or minimum end of the scale.
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
12
Best Answer

Answer by Eric5h5 · Jun 27, 2012 at 07:25 PM

Just divide by 20, or you can use Mathf.InverseLerp for arbitrary ranges. Clamp won't work because it just clamps numbers, it doesn't try to scale anything.

Comment
Add comment · Show 2 · 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 heaversm · Jun 27, 2012 at 07:40 PM 0
Share

Thanks, Lerp should work great.

avatar image meat5000 ♦ · Jul 23, 2015 at 11:58 AM 0
Share

http://answers.unity3d.com/questions/1012209/algorithm-help.html

Similar question, answered with Clamp.

avatar image
14

Answer by hideouswill · Jun 27, 2012 at 08:40 PM

Mathf.Clamp() does not scale a value; it restricts the value to the range. For example, if you clamp between (0, 1), any value greater than 1 will yield a clamped value of 1, and any value less than zero will yield zero; for a value inside the clamp range, the value will be unchanged.

To scale, you need to divide your raw value by the total range, and account for an offset if min != 0. For a range of (min, max):

 scaledValue = (rawValue - min) / (max - min);

For the common case where min == 0:

 scaledValue = rawValue / max;
Comment
Add comment · Show 7 · 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 TechSupportIncoming1 · Jul 23, 2015 at 09:42 AM 0
Share

In a case where max could be theoretically infinite, should I just use an arbitrary big number as a max value?

avatar image saschandroid · Jul 23, 2015 at 09:57 AM 1
Share

c#:

 float max = float.$$anonymous$$axValue; 


avatar image Bunny83 · Jul 23, 2015 at 11:22 AM 2
Share

@TechSupportInco$$anonymous$$g1:
It makes no sense to map an unbounded value to a bounded range. If "1.0" should represent infinity and "0.0" represent "0", then all values would mathematically map to "0.0".

Think about a real world example: temperature. The temperature has a lower bound of 0 kelvin (-273.15 °C) but is unbounded at the top. Why would you want to map an arbitrary temperature into the range of [0.0, 1.0]. All commonly used temperatures would be zero or extremely close to zero. So even temperatures around 1 million kelvin would result to be almost 0.

In such cases you might use a non linear mapping like (1/x) or log():

 v1 = 1f - 1f / (1f+t);


That's the mapping of the first values:

 t       v1        v2
 -----------------------------
 0       0         0
 1       0.5       0.00990
 2       0.66666   0.01960
 3       0.75      0.02912
 4       0.8       0.03846
 5       0.83333   0,04761
 6       0.85714   0.05660
 42      0.97674   0.29577
 100     0.99009   0.5
 333     0.99699   0.76905
 1234    0.99919   0.92503
 +inf    1.0       1.0

As you can see the values between 0 - 1 are mapped to 0-0.5 and the values between 1 - infinity is mapped to 0.5-1.0. If you need a better resolution for values around 100 you can either divide t by 100 beforehead or use 100f ins$$anonymous$$d of 1f:

 v2 = 1f - 100f / (100f+t)

This will make 100 to map to 0.5

Here's a google link to that formula. You should see a graph at the top. make sure you zoom appropriately.

avatar image meat5000 ♦ · Jul 23, 2015 at 11:33 AM 0
Share

It makes no sense to map an unbounded value to a bounded range

(You might need to in a shader)

avatar image TechSupportIncoming1 · Jul 29, 2015 at 10:17 AM 0
Share

@Bunny83 thanks for the elaborate and scientific answer :)

Show more comments
avatar image
1

Answer by AnotherYeti · Jun 27, 2012 at 08:40 PM

If the range you're getting is exactly between 0 and 20, then replace your third line with:

 scaleVal = audioVal / 20.0;

The code you're using isn't working because Mathf.Clamp merely restricts values to the range given, rather than doing a transformation from one space to another.

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
0

Answer by Split3 · Aug 12, 2019 at 04:32 PM

Just in case you want to stop at some point, this code works just perfectly for that!!!

 var scaledValue = Mathf.Clamp(value / maxValue, minValue, maxValue);


Enjoy

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 Bunny83 · Aug 13, 2019 at 11:31 AM 0
Share

This makes no sense. Since "value" is supposed between $$anonymous$$Value and maxValue, dividing value by maxValue gives you a range of 0-1. Clamping that range between $$anonymous$$ and max makes no sense. Also your lower bound would be wrong. You don't seem to be interested in a 0-1 range but in a $$anonymous$$Value to maxValue range which was not asked in the question. Scaling by maxValue makes no sense at all. There are cases where something like this is wanted:

 $$anonymous$$athf.Clamp(value * scale, $$anonymous$$Value, maxValue);

where scale controls the scaling and clamp just limits the outcome to a certain range. However as I said this has nothing to do with the question that was asked.

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

A problem with intersection detection 1 Answer

getting a sum from two scripts 1 Answer

Clarification on how RangeAttribute works 1 Answer

On Button press Move Value toward 2 Answers


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