- Home /
Changing Animation Controller at runtime?
Hello! I'm trying to encapsulate my game's weapons systems and I'm getting hung up on how to self-contain each weapon's character animations.
The goal is that I would be able to define any kind of weapon with a ScriptableObject containing the weapon parameters (model, projectile settings, muzzle flash settings, sound settings, etc.). In a "real" game, I would just create a WeaponData ScriptableObject, fill in the settings, then have everything just work.
I've gotten the majority of the work done, but I'm stuck on animations. I'm using humanoid Mechanim animations for everything, but I don't want to have to define every single possible weapon in one master AnimationController. Ideally, each weapon would have its own AnimationController containing just the animations for the use of that weapon, and if a player changes weapons I just replace the current weapon's AnimationController with the new weapon's AnimationController.
In a perfect world, I'd actually just like to replace one layer in the current AnimationController -- my "weapons" layer, which has a mask on it that allows my underlying walk animations to bleed through. However, if there's no way for me to just change one layer on an AnimationController, I'd be fine with having weapon-specific AnimationControllers and just swapping them out as needed (even if it means I would have to duplicate the underlying walk layer).
Now, I think I can simply just have my WeaponData ScriptableObject contain a reference to a regular AnimationController that I set up. When I swap weapons, I'd use Object.Instantiate
to Instantiate that AnimationController as a RuntimeAnimationController, then assign that to my Animator. However, I have no idea if that's actually how it would work or if the RuntimeAnimationController needs to be some sort of special asset.
An alternative would be to use a AnimatorOverrideController, but I have no idea how that works or how I specify what clips I want to override. If I were to use a AnimatorOverrideController, I'd like to just have "unarmed" animations that I can override with weapon-specific animations as needed. The problem is I have no idea how AnimatorOverrideControllers work, and the documentation isn't very helpful (other than telling me that it can override clips in a controller to specialize animations for a certain avatar -- which sounds like what I want to do, but I'm not exactly sure). Would an AnimatorOverrideController be able to selectively override just my weapon animations? How would I tell it which animations I want to override, and which animations I want to override them with? Would it impact any animations that I don't want to override (i.e. walking)?
Hopefully I'm making some kind of sense here. Is my thinking correct, or is there a better way to handle this?
Your idea sounds like it would work (with the runtimeAnimatorcontroller). Do you need to use the whole animator layer or just the clips? You can tell an animator to replace certain clips with other clips using an AnimatorOverrideController.
http://docs.unity3d.com/ScriptReference/AnimatorOverrideController.html
http://docs.unity3d.com/ScriptReference/AnimatorOverrideController-clips.html
The clips array is a public array(visible in inspector) of animationclipPAIRs, stored in your AnimationOverrideController.
http://docs.unity3d.com/ScriptReference/AnimationClipPair.html
The animationclipPAIR: contains two clips, the original, and the one to override. So for each of your wepons the originalClip would be the same, and the override clip would be specific to the weapon.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183648im_/https://answers.unity.com/themes/thub/images/avi.jpg)