- Home /
How to change animation clips of an animator state at runtime? Is there a way?
So I have 11 unique characters which are playable, as well as can be bots in VS CPU mode! I have 2 characters while others are still in production. I'm using mechanim for animation transitions. I want all of them to use a single animator.
Can I change the animation clips in an animator state at runtime? Because I still need to create animation clips for each character individually (which is fine), but I don't want to create animator for each of them again! I want to use a sinlgle animator!
Answer by WoofyWyzpets · May 01, 2017 at 02:21 PM
You can change animation clips in animators at runtime using a class called RuntimeAnimatorController.
How I used it: I have GO with an animator, it only has 1 animation but I want to swap the clip at runtime. (I have lots of very similar animations in structure and timing, just like different sprites, and I don't feel like making dozens of extremely similar animators just because the sprites change)
This script changes ALL animations in the animator into the animation clip specified in the inspector. You can change multiple animations individually by checking their name in the foreach loop or something.
// Animator animator (get component blablabla)
// AnimationClip anim (field in inspector)
if(anim)
{
AnimatorOverrideController aoc = new AnimatorOverrideController(animator.runtimeAnimatorController);
var anims = new List<KeyValuePair<AnimationClip, AnimationClip>>();
foreach (var a in aoc.animationClips)
anims.Add(new KeyValuePair<AnimationClip, AnimationClip>(a, anim));
aoc.ApplyOverrides(anims);
animator.runtimeAnimatorController = aoc;
}
Original idea thread, apparently there are no other way to change clip yet: https://forum.unity3d.com/threads/change-animation-controller-state-motion-clip-at-runtime-in-unity-5.345142/#post-2291758
this is the best answer. You should also check the docs here, there's another example, and explains it some more: https://docs.unity3d.com/ScriptReference/AnimatorOverrideController.html
Answer by idbrii · Jan 30, 2018 at 12:24 AM
Instead of specifying override clips in code, you can use the AnimatorOverrideController asset.
The AnimatorOverrideController Manual page describes how to setup an AnimatorOverrideController asset.
You can swap to it in code with:
public AnimatorOverrideController CharacterAnimator;
// Call this function from somewhere.
void SwapToOverride()
{
GetComponent<Animator>().runtimeAnimatorController = CharacterAnimator;
}
I believe this is the first method AnimatorOverrideController ScriptReference refers to when it says:
1 . Create an Animator Override Controller in the Editor.
Answer by Adeel1 · Feb 27, 2017 at 09:23 AM
@dakshehs1010 Yes it is possible if you are using animator or animation component for your character. Put multiple animations in ANIMATOR window. You need to set the transition states from one animation to other animation in animator window including the default state. Set the transition conditions that could be of bool, int or string type. Use those transition conditions in your script to change the animations on runtime. e.g Public Animator anim; anim.setBool("YourAnimationName", true/false);
Answer by dakshesh1010 · Feb 27, 2017 at 09:59 AM
Thanks @Adeel1 for responding, yes I've already done that. The problem is something else. My bad I guess. I couldn't explain my problem more elaborately.
The problem is, I want to change the clips stored in the states of the animator at run-time so that I don't have to make new animators for each and every character. I want to use just one animator across all characters, which is not possible unless the animation clip in all the states of animator is changed every time the character is changed!
Answer by AlharbiSunds · Jan 10 at 01:06 PM
I know its old question, but I was looking for the same. I found this tutorial, It seems will work https://www.youtube.com/watch?v=VHYke1HrlMY
https://www.youtube.com/watch?v=wip2kzaF728
https://docs.unity3d.com/ScriptReference/AnimatorOverrideController.ApplyOverrides.html
Your answer
Follow this Question
Related Questions
Why do my animations only play once? 1 Answer
Can animation clips be swapped out of the animator override controller in runtime? 1 Answer
Get weird issue when change from Mechanim -> Ragdoll 0 Answers
Animator Override Controller changed at runtime doesn't always play the animations correctly 1 Answer