Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tenpn
Created July 26, 2012 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenpn/3180757 to your computer and use it in GitHub Desktop.
Save tenpn/3180757 to your computer and use it in GitHub Desktop.
MapValue() takes an input in one range and remaps it to another. Very useful for AI and scripting.
public static float MapValue(float inVal, float inFrom, float inTo, float outFrom, float outTo)
{
float inScale = (inFrom != inTo)
? ( ( inVal - inFrom ) / ( inTo - inFrom ) )
: 0.0f;
float outVal = outFrom + ( inScale * ( outTo - outFrom ) );
outVal = (outFrom < outTo )
? SomeClampFunction( outVal, outFrom, outTo )
: SomeClampFunction( outVal, outTo, outFrom );
return outVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment