- Home /
Bake Animation during Run-time in Unity
I'm wondering if it would be possible to bake an animation to a AnimationCurve during runtime?
I've attempted to assign keyframes on-the-fly using a counter variable as keyframe[counter], but KeyFrame doesn't support iteration (IEnumberable). Is there any way to do this without having to manually type out keyframe[#] for each one?
Is there a better way to bake transformations?
I understand that I could keep track of values over time, but that would also require that "time" be running in order to play it back. Unfortunately I need to have the TimeScale at 0. I currently grab all the animation clips in the scene, and then use a slider where I can scrub the playback, where the slider sets the frame number for each clip. That is for animations created ahead of time. I need to be able to do this with dynamic animation created while the game is running. Hopefully that makes sense.
Thanks!
@bunny83 I think you may have helped me understand something that I was doing wrong. I'll post back. If it doesn't work I'll probably try the Add$$anonymous$$ey functions.
Answer by DaveA · Aug 01, 2012 at 02:26 AM
I would think you would use AddKey and related functions: http://docs.unity3d.com/Documentation/ScriptReference/AnimationCurve.AddKey.html Is that not working?
Answer by Bunny83 · Aug 01, 2012 at 02:39 AM
The most flexible way to set / change KeyFrames in an Animationcurve is to use AnimationCurve.keys. You have to copy them into a local array, modify the array and assign it back.
I'm still not sure why you need to create them on-the-fly. You can Sample an animation at any time you like. You can drive the animation time manually if you want.