c# last frame of animation is reset when the new animation is played

Hi,

I’m trying to procedurally define an animation that would move an object along X first and along Y next. here’s how I do it:

AnimationCurve curve = AnimationCurve.Linear(0, 10, 2, 20);
AnimationClip clip = new AnimationClip();
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);	

AnimationClip clip2 = new AnimationClip();
AnimationCurve curve2 = AnimationCurve.Linear(0, 1, 2, 3);
clip2.SetCurve("", typeof(Transform), "localPosition.y", curve2);
// along X
animation.AddClip(clip, "test");
bool res = animation.Play("test");
// along Y next
board.animation.AddClip(clip2, "test2");
board.animation.PlayQueued("test2");

Both animationa play sequentially but after the object’s been moved along X it’s last position is reset to its initial position and then Y animation is played.
How can I still have the last frame of the animtion be applied once the next anim starts playing? I also tried using clip.wrapMode = WrapMode.ClampForever for the first anim but in this case the second one is never played.

thanks!

As far as i know you can’t animate only one axis of a vector3. Since localPosition is a property you always have to set all members. So i guess it’s not possible to just animation a single axis without touching the others.

You could use two cascaded objects, one for x movement and one for y, but that’s a bit hacky :wink:

Second solution might be to add another curve to your second animation which “holds” the x where it should be

curve = AnimationCurve.Linear(0, 20, 2, 20);
clip2.SetCurve("", typeof(Transform), "localPosition.y", curve2);
clip2.SetCurve("", typeof(Transform), "localPosition.x", curve);

In this case it’s not flexible. The second animation will set x to 20, no matter where it was before.

Or alternatively you could animate it yourself. You still can use the curves if you want but you wouldn’t use the Animation component or the AnimationClips in this case.

Just use AnimationCurve.Evaluate in Update or a coroutine and set the required values manually.