Skip to content

Instantly share code, notes, and snippets.

@Fenikkel
Last active May 13, 2025 11:57
Show Gist options
  • Save Fenikkel/5e51325a3cec827e81143f416f592dc9 to your computer and use it in GitHub Desktop.
Save Fenikkel/5e51325a3cec827e81143f416f592dc9 to your computer and use it in GitHub Desktop.
Unity interpolation

Interpolation for Unity

 

Notes

Simple tu use and foolproof. It uses abstraction to make it almost invisible on your code and easy to reuse it.

 

Usage

  1. Use the enum Ease to select the interpolation type:
    Ease interpolationType = Ease.InOutSine;
  1. Use the static function Interpolation.GetStep to get the interpolated step:
    float interpolatedStep = Interpolation.GetStep(0.5f, Ease.InCirc);
  1. Create an animation with them:
private IEnumerator RotateCoroutine(Transform transform, Quaternion targetRotation, float rotateTime, Ease interpolation)
{
    Quaternion originalRot = transform.rotation;

    float progress = 0f;
    float step;
    Quaternion newRot;

    do
    {
        progress += Time.deltaTime / rotateTime;
        progress = Mathf.Clamp01(progress);

        step = Interpolation.GetStep(progress, interpolation);

        newRot = Quaternion.Lerp(originalRot, targetRotation, step);

        transform.rotation = newRot;

        yield return null;
    }
    while (progress < 1f);
}

 

Credits

 

Compatibility

  • Any Unity version
  • Any pipeline (Build-in, URP, HDRP, etc)

 

Support

⭐ Star if you like it
❤️️ Follow me for more

using UnityEngine;
/* Robert Penner’s formulas: https://easings.net/ */
public static partial class Interpolation
{
public static float GetStep(float step, Ease ease = Ease.Linear)
{
if (step < 0f || 1f < step )
{
Debug.LogWarning("Step is not in 0–1 range");
step = Mathf.Clamp01(step);
}
switch (ease)
{
case Ease.Linear: return Linear(step);
/* Sine */
case Ease.InSine: return EaseInSine(step);
case Ease.OutSine: return EaseOutSine(step);
case Ease.InOutSine: return EaseInOutSine(step);
/* Quad */
case Ease.InQuad: return EaseInQuad(step);
case Ease.OutQuad: return EaseOutQuad(step);
case Ease.InOutQuad: return EaseInOutQuad(step);
/* Cubic */
case Ease.InCubic: return EaseInCubic(step);
case Ease.OutCubic: return EaseOutCubic(step);
case Ease.InOutCubic: return EaseInOutCubic(step);
/* Quart */
case Ease.InQuart: return EaseInQuart(step);
case Ease.OutQuart: return EaseOutQuart(step);
case Ease.InOutQuart: return EaseInOutQuart(step);
// Quint
case Ease.InQuint: return EaseInQuint(step);
case Ease.OutQuint: return EaseOutQuint(step);
case Ease.InOutQuint: return EaseInOutQuint(step);
/* Expo */
case Ease.InExpo: return EaseInExpo(step);
case Ease.OutExpo: return EaseOutExpo(step);
case Ease.InOutExpo: return EaseInOutExpo(step);
/* Circ */
case Ease.InCirc: return EaseInCirc(step);
case Ease.OutCirc: return EaseOutCirc(step);
case Ease.InOutCirc: return EaseInOutCirc(step);
/* Back */
case Ease.InBack: return EaseInBack(step);
case Ease.OutBack: return EaseOutBack(step);
case Ease.InOutBack: return EaseInOutBack(step);
/* Elastic */
case Ease.InElastic: return EaseInElastic(step);
case Ease.OutElastic: return EaseOutElastic(step);
case Ease.InOutElastic:return EaseInOutElastic(step);
/* Bounce */
case Ease.InBounce: return EaseInBounce(step);
case Ease.OutBounce: return EaseOutBounce(step);
case Ease.InOutBounce:return EaseInOutBounce(step);
default:
Debug.LogWarning("Interpolation type not implemented");
return step;
}
}
}
public enum Ease
{
Linear,
/* Sine */
InSine,
OutSine,
InOutSine,
/* Quad */
InQuad,
OutQuad,
InOutQuad,
/* Cubic */
InCubic,
OutCubic,
InOutCubic,
/* Quart */
InQuart,
OutQuart,
InOutQuart,
/* Quint */
InQuint,
OutQuint,
InOutQuint,
/* Expo */
InExpo,
OutExpo,
InOutExpo,
/* Circ */
InCirc,
OutCirc,
InOutCirc,
/* Back */
InBack,
OutBack,
InOutBack,
/* Elastic */
InElastic,
OutElastic,
InOutElastic,
/* Bounce */
InBounce,
OutBounce,
InOutBounce
}
using UnityEngine;
public static partial class Interpolation
{
const float PI = Mathf.PI;
const float HALF_PI = PI * 0.5f;
const float TWO_PI = PI * 2f;
const float C1 = 1.70158f;
const float C2 = C1 * 1.525f;
const float C3 = C1 + 1f;
const float C4 = TWO_PI / 3f;
const float C5 = TWO_PI / 4.5f;
const float N1 = 7.5625f;
const float D1 = 2.75f;
private static float Linear(float t)
{
return t;
}
/* Sine */
private static float EaseInSine(float t)
{
return 1f - Mathf.Cos(t * HALF_PI);
}
private static float EaseOutSine(float t)
{
return Mathf.Sin(t * HALF_PI);
}
private static float EaseInOutSine(float t)
{
return -(Mathf.Cos(PI * t) - 1f) / 2f;
}
/* Quad */
private static float EaseInQuad(float t)
{
return t * t;
}
private static float EaseOutQuad(float t)
{
return 1f - EaseInQuad(1f - t);
}
private static float EaseInOutQuad(float t)
{
if (t < 0.5f)
{
return EaseInQuad(t * 2f) / 2f;
}
else
{
return 1f - EaseInQuad((1f - t) * 2f) / 2f;
}
}
/* Cubic */
private static float EaseInCubic(float t)
{
return t * t * t;
}
private static float EaseOutCubic(float t)
{
return 1f - EaseInCubic(1f - t);
}
private static float EaseInOutCubic(float t)
{
if (t < 0.5f)
{
return EaseInCubic(t * 2f) / 2f;
}
else
{
return 1f - EaseInCubic((1f - t) * 2f) / 2f;
}
}
/* Quart */
private static float EaseInQuart(float t)
{
return t * t * t * t;
}
private static float EaseOutQuart(float t)
{
return 1f - EaseInQuart(1f - t);
}
private static float EaseInOutQuart(float t)
{
if (t < 0.5f)
{
return EaseInQuart(t * 2f) / 2f;
}
else
{
return 1f - EaseInQuart((1f - t) * 2f) / 2f;
}
}
/* Quint */
private static float EaseInQuint(float t)
{
return t * t * t * t * t;
}
private static float EaseOutQuint(float t)
{
return 1f - EaseInQuint(1f - t);
}
private static float EaseInOutQuint(float t)
{
if (t < 0.5f)
{
return EaseInQuint(t * 2f) / 2f;
}
else
{
return 1f - EaseInQuint((1f - t) * 2f) / 2f;
}
}
/* Expo */
private static float EaseInExpo(float t)
{
if (t == 0f)
{
return 0f;
}
else
{
return Mathf.Pow(2f, 10f * (t - 1f));
}
}
private static float EaseOutExpo(float t)
{
if (t == 1f)
{
return 1f;
}
else
{
return 1f - EaseInExpo(1f - t);
}
}
private static float EaseInOutExpo(float t)
{
if (t < 0.5f)
{
return EaseInExpo(t * 2f) / 2f;
}
else
{
return 1f - EaseInExpo((1f - t) * 2f) / 2f;
}
}
/* Circ */
private static float EaseInCirc(float t)
{
return 1f - Mathf.Sqrt(1f - t * t);
}
private static float EaseOutCirc(float t)
{
return Mathf.Sqrt(1f - (t - 1f) * (t - 1f));
}
private static float EaseInOutCirc(float t)
{
if (t < 0.5f)
{
return (1f - Mathf.Sqrt(1f - (2f * t) * (2f * t))) / 2f;
}
else
{
return (Mathf.Sqrt(1f - (-2f * t + 2f) * (-2f * t + 2f)) + 1f) / 2f;
}
}
/* Back */
private static float EaseInBack(float t)
{
return C3 * t * t * t - C1 * t * t;
}
private static float EaseOutBack(float t)
{
float p = t - 1f;
return 1f + C3 * p * p * p + C1 * p * p;
}
private static float EaseInOutBack(float t)
{
if (t < 0.5f)
{
return (Mathf.Pow(2f * t, 2f) * ((C2 + 1f) * 2f * t - C2)) / 2f;
}
else
{
float p = 2f * t - 2f;
return (Mathf.Pow(p, 2f) * ((C2 + 1f) * p + C2) + 2f) / 2f;
}
}
/* Elastic */
private static float EaseInElastic(float t)
{
if (t == 0f)
{
return 0f;
}
if (t == 1f)
{
return 1f;
}
return -Mathf.Pow(2f, 10f * t - 10f) * Mathf.Sin((t * 10f - 10.75f) * C4);
}
private static float EaseOutElastic(float t)
{
if (t == 0f)
{
return 0f;
}
if (t == 1f)
{
return 1f;
}
return Mathf.Pow(2f, -10f * t) * Mathf.Sin((t * 10f - 0.75f) * C4) + 1f;
}
private static float EaseInOutElastic(float t)
{
if (t == 0f)
{
return 0f;
}
if (t == 1f)
{
return 1f;
}
if (t < 0.5f)
{
return -(Mathf.Pow(2f, 20f * t - 10f) * Mathf.Sin((20f * t - 11.125f) * C5)) / 2f;
}
else
{
return (Mathf.Pow(2f, -20f * t + 10f) * Mathf.Sin((20f * t - 11.125f) * C5)) / 2f + 1f;
}
}
/* Bounce */
private static float EaseOutBounce(float t)
{
if (t < 1f / D1)
{
return N1 * t * t;
}
else if (t < 2f / D1)
{
t -= 1.5f / D1;
return N1 * t * t + 0.75f;
}
else if (t < 2.5f / D1)
{
t -= 2.25f / D1;
return N1 * t * t + 0.9375f;
}
else
{
t -= 2.625f / D1;
return N1 * t * t + 0.984375f;
}
}
private static float EaseInBounce(float t)
{
return 1f - EaseOutBounce(1f - t);
}
private static float EaseInOutBounce(float t)
{
if (t < 0.5f)
{
return (1f - EaseOutBounce(1f - 2f * t)) / 2f;
}
else
{
return (EaseOutBounce(2f * t - 1f) + 1f) / 2f;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment