- Home /
Other
How can I make my Animator State's Motion looping dynamically?
I am dynamically creating Animation Clips and assigning them to an Animator at runtime. The Animator itself never changes, however the animations may need to change depending on the circumstances of the game. For instance, during one animation the object may need to animate from 0,0,0 to 0,15,0 along the Y-Axis. The next time I ask my Animator to play I need the same animation but this time it should move from 0,0,0 to 0,32,0 or something random like that. The gist is that the Animator will remain, the states won't change, but the animations will be slightly altered, rebuilt, and reassigned before I tell the object to animate again.
Now on to the problem! I have successfully done this all and reassigned my animation clip, that part was alright. The problem that I have now is that when I was animating with a static animation that I had generated by hand it was easy to tell it to loop that one second animation ten times, I just changed the Exit Time to 10.0. Now when I add my auto-generated animation, the Exit Time is still 10 seconds however the animation itself only ever plays once, despite having marked it as WrapMode.Loop while creating it. I'll post as much relevant code as I can to try and help people understand what I'm going for here.
var newAnimationClip = new AnimationClip();
var curve = AnimationCurve.Linear(0, 0, 1 / speed, moveToPosition);
newAnimationClip.SetCurve("", typeof(Transform), "localPosition.y", curve);
AnimationUtility.SetAnimationType(newAnimationClip, ModelImporterAnimationType.Human);
newAnimationClip.wrapMode = WrapMode.Loop;
GetStateFromObject(go, "MyState").SetAnimationClip( newAnimationClip );
Thanks for any and all help!
Edit : So the approach I am going to attempt is to not reuse already created transitions, but generate my own and hope that during this process it will automatically loop the motion appropriately for the exit time. I'll update this if I find success doing so.
Edit2 : Yeah that really did not work. I don't think there was ever any intent in Unity 4 for people to edit Animator and their States' motions the way I'm trying to and it's going to end up being way more work to get this to function properly than is worth.
Follow this Question
Related Questions
how to go to last frame of animation clip immediately? 1 Answer
Getting the position of an object in an AnimationClip ? 1 Answer
Edit AnimationClip during runtime from script 0 Answers
animation to animator [mecanim] 1 Answer
Is there a way to change the WrapMode of all Animations in a Mecanim Animator? 1 Answer