Version: 2023.2
LanguageEnglish
  • C#

Mathf.InverseLerp

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Declaration

public static float InverseLerp(float a, float b, float value);

Parameters

a The start of the range.
b The end of the range.
value The point within the range you want to calculate.

Returns

float A value between zero and one, representing where the "value" parameter falls within the range defined by a and b.

Description

Determines where a value lies between two points.

The a and b values define the start and end of a linear numeric range. The "value" parameter you supply represents a value which might lie somewhere within that range. This method calculates where, within the specified range, the "value" parameter falls.
If the "value" parameter is within the range, InverseLerp returns a value between zero and one, proportional to the value's position within the range. If the "value" parameter falls outside of the range, InverseLerp returns either zero or one, depending on whether it falls before the start of the range or after the end of the range.

using UnityEngine;

public class ExampleClass : MonoBehaviour { public float start = 20.0f; public float end = 40.0f; public float currentProgress = 22.0f;

void Start() { // the progress between start and end is stored as a 0-1 value, in 'i' float i = Mathf.InverseLerp(start, end, currentProgress);

// this will display "Current progress: 0.1 or 10%" in Console window Debug.Log("Current progress: " + i + " or " + i * 100 + "%");

// the needle of an on-screen dial could then be set to a // rotational angle out of 360 degrees, based on the progress. float dialNeedleAngle = i * 360;

//// this will display "Needle angle: 36" in Console window Debug.Log("Needle angle: " + dialNeedleAngle); } }

See Also: Lerp.

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961