- Home /
How AnimationCurve.MoveKey works?
Hey there.
I'm writing a timeline editor, using AnimationCurves, and in this editor, I want to be able to move the keyframes over time. I could use the default AnimationCurve editor, but I need to have a view of the timeline's range for all keyframes of all curves at a same time to be able to sync different curves together.
One way to move a keyframe within the time is by deleting it and adding it with its new time, but it is what the function MoveKey specificaly do.
BUT, while using it, it delete some keyframes without adding them, and I don't understand the return value.
The documentation is also reaaaally minimalist, without example or whatsoever : http://docs.unity3d.com/Documentation/ScriptReference/AnimationCurve.MoveKey.html
So, does anyone here understood the way one can move a keyframe safely? (and in another way, multiple keyframes alltogether?)
public AnimationCurve curve;
// ...
for(int key = 0; key < curve.length; key++)
{
Keyframe temp = curve[key];
temp.time = GetNewTime();
curve.MoveKey(key, temp);
}
Editor exclusively.
I'm trying to set the time of multiple keyframes at a time.
Answer by Bunny83 · Aug 28, 2013 at 05:04 PM
An AnimationCurve has to order all it's keyframes by the keyframes time. If you change the time of a keyframe to a value of another keyframe those keyframes will be "merged" since it's not possible to have two keyframes at the exact same time.
I never used MoveKey, but from the description it seems it takes care of the sorting and merge the keyframe with another one if they share the same time.
The function should in any case return the new index of the keyframe.
Your for loop is a bit of a risk since MoveKey can change the order of the array. You should use the keys property instead.
store a local copy of your keyframes, change them as you like and assign them back. When you assign the temp array back the keyframes will be sorted automatically.
And what about the returned index? It sometimes return -1.
Your answer
Follow this Question
Related Questions
How can I set the tangents of Keyframes in an AnimationCurve through scripting? 1 Answer
Tangents ignored while setting keyframes 1 Answer
Move a keyframe in time only 1 Answer
How can I make an animation curve Keyframe "Auto" in code? 3 Answers
Accessing Animation Curves/Keyframes during runtime 1 Answer