- Home /
How can I set the tangents of Keyframes in an AnimationCurve through scripting?
I want to set both tangents of a Keyframe in an AnimationCurve through scripting, just like it is possible to do in the Animation view in Unity. How can I do it in code?
Answer by runevision · Nov 05, 2009 at 01:25 PM
If you have an AnimationCurve called myAnimationCurve, you can get a Keyframe from this curve and set the tangents of that Keyframe like this:
// 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);
You can also smooth out the tangents of a Keyframe by using SmoothTangents on the AnimationCurve:
// Smooth the tangents of the fourth key in the curve
myAnimationCurve.SmoothTangents(3, 0);
The second parameter, weight, controls the contribution of the two tangents in the smoothed result. -1 makes the smooth result equal to what the inTangent was before; 1 makes it equal to what the outTangent was, and 0 produces an average.
can i use myAnimationCurve.keys[3].inTangent = 1 directly?? and can you plz explain the SmoothTangents i just cant get it does it take only 0,-1,1??? thxxx
No you can't set myAnimationCurve.keys[3].inTangent directly since it would just modify a struct which is never assigned back into the curve. SmoothTangents takes a float that can be anything in between -1 and 1.