Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
2
Question by Josecond1 · Dec 30, 2016 at 02:44 AM · lerpmathmathfinterpolationinverse

What exactly does Mathf.InverseLerp() do?

Example Mathf.InverseLerp(5.0f, 10.0f, 8.0f) this return 0.6, whats mean this value returned?

Unity doc say me that this value is inear parameter t that produces the interpolant value within the range [a, b]. What does that mean?

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
8

Answer by elenzil · Dec 30, 2016 at 06:41 PM

it's saying that 8 is 60% of the way from 5 to 10.

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
1

Answer by orionsyndrome · Feb 03, 2019 at 07:47 PM

If you need to find how far the value is in respect with two fixed values (obviously as a ratio, which can be thought of as a percentage), you need a Lerp, which is short for 'linear interpolation'.

Simply put, if you think about (i.e.) point1 at 0% and some other point2 at 100%, there is a simple way to find any other point in between these two if you imagine a line between them.

If you think about it, you can express all points on this line as point1 + offset. If your offset is 0, then point1 + 0 and you're exactly at point1. If your offset is equal to difference, where difference = (point2 - point1), then you arrive at point2 (because point1 + (point2 - point1 )). Conveniently, you can introduce a multiplier to this difference, and so you come up with a formula point1 + factor x (point2 - point1) where a value of 0 gives you point1, a value of 1 gives you point2, and 0.5 (or 50%), gives you exactly the midpoint between the two.

This is what Lerp does.

However, sometimes we need to do the opposite, we need that multiplier when we only had a concrete value. For this we solve this equation backwards.

 value = point1 + factor x (point2 - point1), where value is known, but the factor is unknown
 factor x (point2 - point1) = value - point1, now we divide the whole thing by (point2 - point1)
 factor = (value - point1) / (point2 - point1)

And this is what InverseLerp does.

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 orionsyndrome · Feb 03, 2019 at 08:02 PM 0
Share

Oh btw because of this division, you might want to make sure the deno$$anonymous$$ator isn't zero (if for some reason you're implementing this math on your own).

If it's zero, that means the points coincide in space, and thus the line cannot be defined properly.. Therefore, we need a check: if($$anonymous$$athf.Abs(point2 - point1) < epsilon) return point1;

This check is due to floating point precision errors, as you cannot simply check for zero and be absolutely sure that the difference will be exactly zero.

Epsilon is typically a very small value in the order of 1E-5 (that's 10^-5). Or, if you're working with Vector3, you could use the provided kEpsilon.

With this line added, this is exactly what InverseLerp does for you.

 float InverseLerp(float $$anonymous$$, float max, float value) {
   if($$anonymous$$athf.Abs(max - $$anonymous$$) < epsilon) return $$anonymous$$;
   return (value - $$anonymous$$) / (max - $$anonymous$$);
 }

On the other hand, if the points do coincide, for your implementation you might decide that the result is undefined ins$$anonymous$$d, and throw an error such as ArgumentException (which is a more robust way to handle this in the more general scenario).

avatar image secondimpactrob · Sep 28, 2020 at 10:52 AM 1
Share

Just a note for anyone arriving here via google, the code posted by @orionsyndrome is not quite 100% reflective of Unity's implementation for two reasons:

1) In case where the $$anonymous$$ and max are the same (deno$$anonymous$$ator is zero) the function should return 0 (or 1, implementation choice), and not $$anonymous$$. (I think this is a typo)

2) Unity's InverseLerp clamps the result between 0 and 1.

Unity's implementation looks like this:

 public static float InverseLerp(float a, float b, float value)
 {
     if (a != b)
         return Clamp01((value - a) / (b - a));
     else
         return 0.0f;
  }
avatar image
0

Answer by idbrii · Jul 28, 2021 at 07:53 PM

InverseLerp finds the progress between two values.

For InverseLerp(float a, float b, float value), a is the start, b is the end, value is the current position. It returns how far along the "path" value is from a to b.


For example, you might use it in a flocking algorithm to apply falloff in your Seek behaviour: var seek_influence = InverseLerp(min_distance, max_distance, current_distance) Now you know how strongly you should apply your seek behaviour (0 = don't apply, 1 = maximum power!).

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 Jaakk0S · May 29 at 07:39 AM

This is the most common use case for InverseLerp in Unity:

"When param A goes from 45 to 145 I want param B to go from 30 to 10".

 B = Mathf.Lerp(30f, 10f, Mathf.InverseLerp(45f, 145f, A));

InverseLerp will return a range 0..1 which is perfect input for Lerp. Then again it's perfect input for much anything, such as a probability for something happening.

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

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

92 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

Related Questions

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

Meaning of inWeight and outWeight in Keyframe, or recalculate tangents from sampling 0 Answers

Mathf.Lerp for integers 1 Answer

Lerp between two int vaules smoothly 4 Answers

RotateAround an object with clamped limits 0 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