How does AnimationCurve.SmoothTangents work

Hi,

I’ve been trying to create AnimationCurves on the fly at runtime (btw, how big of an overhead are they? Is there a reason why we don’t get access to stuff like changing keyframes without editor classes?) and I was trying to smooth out the motion (like custom ease in and out) using SmoothTangents, however whatever values I try there doesn’t seem to have any effect. What exactly is it expecting and how should it behave? From the docs it would appear that if I set 0 it would make the tangent horizontal, so I assumed that if I used Sqr(3)/2 it would be a 60º angle, but I don’t see anything change (nor does tangent = 0 seem to me like an ease in / out).

I’m creating a curve with only 2 keyframes, can this be the issue (i.e. not work on the edge keyframes?

And another thing that I didn’t understand is why we only have access to one tangent value if in the Animation Editor we can tweak both? Just a simplification because of runtime processing?

Regards,
Afonso

First of all, you don’t need editor classes to manipulate AnimationCurves (including keyframes) by themselves. You do need editor classes to get the AnimationCurves from an AnimationClip, but that is not related to overhead.

No, SmoothTangents does not set the tangents to 0 or any other specific angle, but rather makes the inTangent and outTangent line up so they have identical slope. Using a weight of 0 will make the final smooth slope an average of what the inTangent and outTangent were before.

Yes, SmoothTangents only really make sense for middle KeyFrames, not the KeyFrame at the start or end of the AnimationCurve.

You can set both by code.

// Get the fourth key in the curve
Keyframe key = myAnimationCurve[3]; 

// Set both tangents to something
key.inTangent = 1;
key.outTangent = 0;

// Put the modified key back into the curve
myAnimationCurve.MoveKey(3, key);

Look at the scripting reference for the classes AnimationCurve and Keyframe for more info:

Hope that helps.

Rune

2 Likes

I also posted a short and concise question and answer here:

http://answers.unity3d.com/questions/163/how-can-i-set-the-tangents-of-keyframes-in-an-animationcurve-through-scripting

Rune

That’s what confused me. Because what I had done was create an AnimationClip with the Animation Editor and then I wanted to tweak its keyframes at runtime, but since I couldn’t get the AnimationCurves I thought it wasn’t possible, that I had limited access at runtime.

Thanks for the explanation.

Regards,
Afonso