Skip to content

Instantly share code, notes, and snippets.

@mikkelens
Last active July 11, 2022 21:44
Show Gist options
  • Save mikkelens/df8aca8a111806439256a1a8caa35051 to your computer and use it in GitHub Desktop.
Save mikkelens/df8aca8a111806439256a1a8caa35051 to your computer and use it in GitHub Desktop.
my helpers for unity animation curves
using UnityEngine;
namespace ScriptTools
{
public static class AnimationCurveHelpers
{
/// <summary>
/// A helper for flipping an animation curve.
/// </summary>
/// <param name="curve"></param>
/// <returns>curve, but flipped vertically.</returns>
public static AnimationCurve FlippedY(this AnimationCurve curve)
{
// create flipped speedcurve
Keyframe[] keys = new Keyframe[curve.length];
for (int i = 0; i < curve.length; i++)
{
Keyframe key = curve.keys[i];
keys[i] = new Keyframe(key.time, 1 - key.value)
{
inTangent = Mathf.Clamp(-key.inTangent, -9999999999f, 9999999999f),
outTangent = Mathf.Clamp(-key.outTangent, -9999999999f, 9999999999f),
weightedMode = key.weightedMode,
inWeight = key.inWeight,
outWeight = key.outWeight
};
}
return new AnimationCurve(keys);
}
/// <summary>
/// A helper for flipping an animation curve.
/// </summary>
/// <param name="curve"></param>
/// <returns>curve, but flipped horizontally.</returns>
public static AnimationCurve FlippedX(this AnimationCurve curve)
{
// create flipped speedcurve
Keyframe[] keys = new Keyframe[curve.length];
for (int i = 0; i < curve.length; i++)
{
Keyframe key = curve.keys[i];
keys[i] = new Keyframe(1 - key.time, key.value)
{
inTangent = Mathf.Clamp(-key.inTangent, -9999999999f, 9999999999f),
outTangent = Mathf.Clamp(-key.outTangent, -9999999999f, 9999999999f),
weightedMode = key.weightedMode,
inWeight = key.inWeight,
outWeight = key.outWeight
};
}
return new AnimationCurve(keys);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment