UnityUtilities
UnityUtilities.MathHelper Class Reference

The MathHelper contains methods to help with mapping and angles and a really nifty method for framerate-independent eased lerping. More...

Static Public Member Functions

static float MapClamped (float sourceValue, float sourceFrom, float sourceTo, float targetFrom, float targetTo)
 Maps a value from [sourceFrom..sourceTo] to [targetFrom..targetTo] with clamping. More...
 
static float ApplyJoystickDeadzone (float value, float deadzone, bool fullRangeBetweenDeadzoneAndOne=false)
 Applies a deadzone [-deadzone..deadzone] in which the value will be set to 0. More...
 
static float MapClampedJoystick (float sourceValue, float sourceFrom, float sourceTo, float deadzone=0f, bool fullRangeBetweenDeadzoneAndOne=false)
 Maps a joystick input from [sourceFrom..sourceTo] to [-1..1] with clamping. Applies a deadzone [-deadzone..deadzone] in which the value will be set to 0. More...
 
static float GetCenterAngleDeg (float angle1, float angle2)
 Returns the closer center between two angles. More...
 
static float NormalizeAngleDeg360 (float angle)
 Normalizes an angle between 0 (inclusive) and 360 (exclusive). More...
 
static float NormalizeAngleDeg180 (float angle)
 Normalizes an angle between -180 (inclusive) and 180 (exclusive). More...
 
static float EasedLerpFactor (float factor, float deltaTime=0f)
 Provides a framerate-independent t for lerping towards a target. More...
 
static float EasedLerp (float current, float target, float percentPerSecond, float deltaTime=0f)
 Framerate-independent eased lerping to a target value, slowing down the closer it is. More...
 

Detailed Description

The MathHelper contains methods to help with mapping and angles and a really nifty method for framerate-independent eased lerping.

Member Function Documentation

§ ApplyJoystickDeadzone()

static float UnityUtilities.MathHelper.ApplyJoystickDeadzone ( float  value,
float  deadzone,
bool  fullRangeBetweenDeadzoneAndOne = false 
)
static

Applies a deadzone [-deadzone..deadzone] in which the value will be set to 0.

Parameters
valueThe joystick value.
deadzoneA value between for which all results [-deadzone..deadzone] will be set to 0.
fullRangeBetweenDeadzoneAndOneIf this is true, the values between [-1..-deadzone] and [deadzone..1] will be mapped to [-1..0] and [0..1] respectively.
Returns
The result value between [-1..1].

§ EasedLerp()

static float UnityUtilities.MathHelper.EasedLerp ( float  current,
float  target,
float  percentPerSecond,
float  deltaTime = 0f 
)
static

Framerate-independent eased lerping to a target value, slowing down the closer it is.

If you call

currentValue = MathHelper.EasedLerp(currentValue, 1f, 0.75f);

each frame (e.g. in Update()), starting with a currentValue of 0, then after 1 second it will be approximately 0.75 - which is 75% of the way between 0 and 1.

Adjusting the target or the percentPerSecond between calls is also possible.

Parameters
currentThe current value.
targetThe target value.
percentPerSecondHow much of the distance between current and target should be covered per second?
deltaTimeHow much time passed since the last call.
Returns
The interpolated value from current to target.

§ EasedLerpFactor()

static float UnityUtilities.MathHelper.EasedLerpFactor ( float  factor,
float  deltaTime = 0f 
)
static

Provides a framerate-independent t for lerping towards a target.

Example:

currentValue = Mathf.Lerp(currentValue, 1f, MathHelper.EasedLerpFactor(0.75f);

will cover 75% of the remaining distance between currentValue and 1 each second.

There are essentially two ways of lerping a value over time: linear (constant speed) or eased (e.g. getting slower the closer you are to the target, see http://easings.net.)

For linear lerping (and most of the easing functions), you need to track the start and end positions and the time that elapsed.

Calling something like

currentValue = Mathf.Lerp(currentValue, 1f, 0.95f);

every frame provides an easy way of eased lerping without tracking elapsed time or the starting value, but since it's called every frame, the actual traversed distance per second changes the higher the framerate is.

This function replaces the lerp T to make it framerate-independent and easier to estimate.

For more info, see https://www.scirra.com/blog/ashley/17/using-lerp-with-delta-time.

Parameters
factorHow much % the lerp should cover per second.
deltaTimeHow much time passed since the last call.
Returns
The framerate-independent lerp t.

§ GetCenterAngleDeg()

static float UnityUtilities.MathHelper.GetCenterAngleDeg ( float  angle1,
float  angle2 
)
static

Returns the closer center between two angles.

Parameters
angle1The first angle.
angle2The second angle.
Returns
The closer center.

§ MapClamped()

static float UnityUtilities.MathHelper.MapClamped ( float  sourceValue,
float  sourceFrom,
float  sourceTo,
float  targetFrom,
float  targetTo 
)
static

Maps a value from [sourceFrom..sourceTo] to [targetFrom..targetTo] with clamping.

This is basically Mathf.Lerp(targetFrom, targetTo, Mathf.InverseLerp(sourceFrom, sourceTo, sourceValue)).

Parameters
sourceValueThe value in the range of [sourceFrom..sourceTo]. Will be clamped if not in that range.
sourceFromThe lower end of the source range.
sourceToThe higher end of the source range.
targetFromThe lower end of the target range.
targetToThe higher end of the target range.
Returns
The mapped value.

§ MapClampedJoystick()

static float UnityUtilities.MathHelper.MapClampedJoystick ( float  sourceValue,
float  sourceFrom,
float  sourceTo,
float  deadzone = 0f,
bool  fullRangeBetweenDeadzoneAndOne = false 
)
static

Maps a joystick input from [sourceFrom..sourceTo] to [-1..1] with clamping. Applies a deadzone [-deadzone..deadzone] in which the value will be set to 0.

Parameters
sourceValueThe value in the range of [sourceFrom..sourceTo]. Will be clamped if not in that range.
sourceFromThe lower end of the source range.
sourceToThe higher end of the source range.
deadzoneA value between 0 and 1 for which all results [-deadzone..deadzone] will be set to 0.
fullRangeBetweenDeadzoneAndOneIf this is true, the values between [-1..-deadzone] and [deadzone..1] will be mapped to [-1..0] and [0..1] respectively.
Returns
The result value between [-1..1].

§ NormalizeAngleDeg180()

static float UnityUtilities.MathHelper.NormalizeAngleDeg180 ( float  angle)
static

Normalizes an angle between -180 (inclusive) and 180 (exclusive).

Parameters
angleThe input angle.
Returns
The result angle.

§ NormalizeAngleDeg360()

static float UnityUtilities.MathHelper.NormalizeAngleDeg360 ( float  angle)
static

Normalizes an angle between 0 (inclusive) and 360 (exclusive).

Parameters
angleThe input angle.
Returns
The result angle.

The documentation for this class was generated from the following file: