- Edited
Lerping an animation in code
I would like to lerp an animation in code like you can with Mathf.Lerp and floats, so that I can directly tie the animation to the action in the code. I would like to know a logical way to to this.
Basically the idea is that you can specify the animation and a variable between 0 and 1, and it will jump to the period in the animation based on the "0 to 1" variable in terms of percentage, just like Mathf.Lerp.
There are two ways of doing this. One is working through AnimationState. Another is working directly on the Animation and Skeleton.
1.
With AnimationState, when you set the animation, you set the TimeScale to 0 so it won't progress automatically and you can set the time yourself.
var trackEntry = skeletonAnimation.AnimationState.SetAnimation(0, "lerped animation", true);
trackEntry.TimeScale = 0f;
float duration = trackEntry.Animation.Duration;
Alternatively, you could also set the TimeScale on the SkeletonAnimation or AnimationState. It's up to you.
Then you set the time on the trackentry whenever:
trackEntry.TrackTime = duration * yourPercent;
2.
There's a sample of working directly with Animation and Skeleton in the Examples folder of the unitypackage. open SpineGauge.cs.
First you find the animation, then you apply it to the skeleton at the given time.
Concisely:
var animation = skeletonAnimation.Skeleton.Data.FindAnimation("lerpedAnimation");
animation.Apply(skeletonAnimation.Skeleton, 0, animation.Duration * yourPercent, false, null, 1f, true, false);
Using this method requires that you fully understand and account for when animations are being applied by your various components, and the effects of manually having animations applied one on top of the other, if ever you want that.
Thank you! Would either of these methods cause me to lose the effects of the mix duration? It would be nice to keep that in order to maintain smooth transitions.