x


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?

more ▼

asked Nov 05 '09 at 01:18 PM

runevision gravatar image

runevision ♦♦
8.1k 29 46 112

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

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.

more ▼

answered Nov 05 '09 at 01:25 PM

runevision gravatar image

runevision ♦♦
8.1k 29 46 112

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

Sep 02 '10 at 04:36 AM vyruz

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.

Sep 02 '10 at 01:03 PM runevision ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5274
x88
x57
x52
x22

asked: Nov 05 '09 at 01:18 PM

Seen: 2764 times

Last Updated: Nov 05 '09 at 01:18 PM