UnityUtilities
|
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... | |
The MathHelper contains methods to help with mapping and angles and a really nifty method for framerate-independent eased lerping.
|
static |
Applies a deadzone [-deadzone..deadzone] in which the value will be set to 0.
value | The joystick value. |
deadzone | A value between for which all results [-deadzone..deadzone] will be set to 0. |
fullRangeBetweenDeadzoneAndOne | If this is true, the values between [-1..-deadzone] and [deadzone..1] will be mapped to [-1..0] and [0..1] respectively. |
|
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.
current | The current value. |
target | The target value. |
percentPerSecond | How much of the distance between current and target should be covered per second? |
deltaTime | How much time passed since the last call. |
|
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.
factor | How much % the lerp should cover per second. |
deltaTime | How much time passed since the last call. |
|
static |
Returns the closer center between two angles.
angle1 | The first angle. |
angle2 | The second angle. |
|
static |
Maps a value from [sourceFrom..sourceTo] to [targetFrom..targetTo] with clamping.
This is basically Mathf.Lerp(targetFrom, targetTo, Mathf.InverseLerp(sourceFrom, sourceTo, sourceValue)).
sourceValue | The value in the range of [sourceFrom..sourceTo]. Will be clamped if not in that range. |
sourceFrom | The lower end of the source range. |
sourceTo | The higher end of the source range. |
targetFrom | The lower end of the target range. |
targetTo | The higher end of the target range. |
|
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.
sourceValue | The value in the range of [sourceFrom..sourceTo]. Will be clamped if not in that range. |
sourceFrom | The lower end of the source range. |
sourceTo | The higher end of the source range. |
deadzone | A value between 0 and 1 for which all results [-deadzone..deadzone] will be set to 0. |
fullRangeBetweenDeadzoneAndOne | If this is true, the values between [-1..-deadzone] and [deadzone..1] will be mapped to [-1..0] and [0..1] respectively. |
|
static |
Normalizes an angle between -180 (inclusive) and 180 (exclusive).
angle | The input angle. |
|
static |
Normalizes an angle between 0 (inclusive) and 360 (exclusive).
angle | The input angle. |