- Home /
How do I modify a keyframe/curve's tangent in code?
Hi, I recently noticed that when i split animations Unity modifies the tangents of some of the keys (usually the first, last and/or second to the last) creating a very noticable pop especially in looped animations.
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
public class FixAnimation : ScriptableObject
{
[MenuItem("Animation/Fix Animation")]
static void ListChildTransforms()
{
GameObject g = Selection.activeGameObject;
Debug.Log(g.name.ToString());
if (g.animation && g.animation.clip)
{
List<string> clips = new List<string>();
foreach (AnimationState state in g.animation)
{
clips.Add(state.name);
}
foreach(string clipName in clips)
{
FixClip(g.animation.GetClip(clipName));
}
}
}
public static void FixClip(AnimationClip clip)
{
AnimationClipCurveData[] curveData = AnimationUtility.GetAllCurves(clip,true);
AnimationCurve anim = null;
for (int i = 0; i < curveData.Length; i++)
{
anim = curveData[i].curve;
anim.SmoothTangents(0,0);
anim.SmoothTangents(anim.length-2,0);
anim.SmoothTangents(anim.length-1,0);
/*
anim.keys[0].outTangent = 0;
anim.keys[0].inTangent = 0;
anim.keys[length-2].outTangent = 0;
anim.keys[length-2].inTangent = 0;
anim.keys[length-1].outTangent = 0;
anim.keys[length-1].inTangent = 0;
*/
//replace existing curve
clip.SetCurve(curveData[i].path, curveData[i].type,curveData[i].propertyName,anim);
}
}
}
I attempted to fix this by code but i am not having much success. It seems when i succed in cleaning up the last frame the first frame suffers. Is there an ordering that need to be followed? what does keyfram.tangentMode control? When editing the animation in the animation view all i need to do is right click on the key and set both tangents to linear and the popping is gone. How do i replicate this in code?
Answer by Bunny83 · Jul 21, 2011 at 11:32 PM
Have you read this:
http://answers.unity3d.com/questions/48590/how-can-i-convert-animationcurveinouttangent-to-an.html
If you want to use Mathf.Tan keep in mind that it works with radians. If you want to use degrees you have to multiply by Mathf.Deg2Rad
I recently tied calculating the linear tangent myself
float o =0,a =0;
anim.keys[0].inTangent = 1;
o = anim.keys[anim.length-1].value - anim.keys[anim.length-2].value;
a = anim.keys[1].time;// always 1 frame
anim.keys[anim.length-2].outTangent = (float) $$anonymous$$ath.Atan2(o,a);
anim.keys[anim.length-1].inTangent = (float) $$anonymous$$ath.Atan2(a,o);
Still no luck
Answer by SinisterMephisto · Jul 22, 2011 at 11:36 AM
Thanks for the reply. But this makes me wonder how do you specify both tangents as linear? Cos it seems it would be some what relative to the last key. maybe making a triangle with the value of previous key current key and the time between them to calculate the outTangent of the last key and the inTangent of the current key.
It seems the curve editor has the functions i need there has to be a way to reuse that function.
Thanks
Your answer
Follow this Question
Related Questions
Can I make animations snap to a frame? 1 Answer
Animation not importing into Unity with the same curves as Maya 5 Answers
How can I set my Animation Curve's Tangents to Flat in script? 0 Answers
Why can't I edit animation curves imported from Maya? 1 Answer
Unity Animation - Loop around a closed loop trajectory 1 Answer