Skip to content

Instantly share code, notes, and snippets.

@INeatFreak
Last active December 19, 2023 18:59
Show Gist options
  • Save INeatFreak/b30b9849793ccfd8c96b19131d756273 to your computer and use it in GitHub Desktop.
Save INeatFreak/b30b9849793ccfd8c96b19131d756273 to your computer and use it in GitHub Desktop.
Remaps the given value from one range to another.
/// <summary>Remaps the given value from one range to another. Source: <a href="https://gist.github.com/INeatFreak/b30b9849793ccfd8c96b19131d756273">GitHub</a> </summary>
/// <param name="source">The value to remap.</param>
/// <param name="sourceMin">Minimum range of the source value.</param>
/// <param name="sourceMax">Maximum range of the source value.</param>
/// <param name="targetMin">Minimum range of the return value.</param>
/// <param name="targetMax">Maximum range of the return value.</param>
public float RemapValueRange(float source, float sourceMin, float sourceMax, float targetMin, float targetMax) {
source = Mathf.Clamp(source, sourceMin, sourceMax); // ensure that source value is in the given range or it can output higher/lower values than requested value range!
return (source - sourceMin) * (targetMax - targetMin) / (sourceMax - sourceMin) + targetMin;
}
/// <summary>Remaps the given value from (0 -> 1) range to requested range. Source: <a href="https://gist.github.com/INeatFreak/b30b9849793ccfd8c96b19131d756273">GitHub</a> </summary>
/// <param name="source">The value to remap.</param>
/// <param name="targetMin">Minimum range of the return value.</param>
/// <param name="targetMax">Maximum range of the return value.</param>
public float RemapValueRangeFrom01(float source, float targetMin, float targetMax) {
return RemapValueRange(source, 0, 1, targetMin, targetMax);
}
/// <summary>Remaps the given value from the given range to (0 -> 1) range. Source: <a href="https://gist.github.com/INeatFreak/b30b9849793ccfd8c96b19131d756273">GitHub</a> </summary>
/// <param name="source">The value to remap.</param>
/// <param name="sourceMin">Minimum range of the source value.</param>
/// <param name="sourceMax">Maximum range of the source value.</param>
public float RemapValueRangeTo01(float source, float sourceMin, float sourceMax) {
return RemapValueRange(source, sourceMin, sourceMax, 0, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment