Skip to content

Instantly share code, notes, and snippets.

@keijiro
Created April 28, 2011 23:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save keijiro/947530 to your computer and use it in GitHub Desktop.
Save keijiro/947530 to your computer and use it in GitHub Desktop.
Exponential easing out utility for Unity
// Example:
//
// currentPos = ExpEase.Out(currentPos, targetPos, -4.0);
//
// or
//
// ExpEase.Out2(currentPos, targetPos, -4.0); // This modifies currentPos.
//
using UnityEngine;
public static class ExpEase {
public static float Out(float current, float target, float coeff) {
return target - (target - current) * Mathf.Exp(coeff * Time.deltaTime);
}
public static float OutAngle(float current, float target, float coeff) {
return target -
Mathf.DeltaAngle(current, target) * Mathf.Exp(coeff * Time.deltaTime);
}
public static Vector3 Out(Vector3 current, Vector3 target, float coeff) {
return Vector3.Lerp(target, current, Mathf.Exp(coeff * Time.deltaTime));
}
public static Quaternion Out(Quaternion current, Quaternion target, float coeff) {
if (current == target) {
return target;
} else {
return Quaternion.Lerp(target, current, Mathf.Exp(coeff * Time.deltaTime));
}
}
public static void Out2(ref float current, float target, float coeff) {
current = target - (target - current) * Mathf.Exp(coeff * Time.deltaTime);
}
public static void OutAngle2(ref float current, float target, float coeff) {
current = target -
Mathf.DeltaAngle(current, target) * Mathf.Exp(coeff * Time.deltaTime);
}
public static void Out2(ref Vector3 current, Vector3 target, float coeff) {
current = Vector3.Lerp(target, current, Mathf.Exp(coeff * Time.deltaTime));
}
public static void Out2(ref Quaternion current, Quaternion target, float coeff) {
if (current == target) {
current = target;
} else {
current = Quaternion.Lerp(target, current, Mathf.Exp(coeff * Time.deltaTime));
}
}
public static void OutLocalTransform(Transform current, Vector3 targetPosition,
Quaternion targetRotation, float coeff) {
coeff = Mathf.Exp(coeff * Time.deltaTime);
current.localPosition = Vector3.Lerp(targetPosition,
current.localPosition, coeff);
if (current.localRotation == targetRotation) {
current.localRotation = targetRotation;
} else {
current.localRotation = Quaternion.Lerp(targetRotation,
current.localRotation, coeff);
}
}
}
@jm991
Copy link

jm991 commented Apr 8, 2013

Awesome! Thank you! Using this in a few of my projects now for camera movement and the like. Solid code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment