Can an AnimatorController play an animation clip stored in another object?

Can I hold an animation clip in a public variable on an object, then have another object play that animation?
Say I have a character, and for that character to interact with a specific object they will need to perform a specific animation. What I would like to do is have the object that is being interacted with specify the animation the character should perform.
I know I can put the animation in the character’s animator controller and set a trigger value, then the object simply provides that value, but I’m looking at lots of potential unique objects and for the sake of modularity I wondered if it was possible for the object to actually say to the character, “Here, play this animation…”.
This animation would only effect the character, not the object being interacted with.

Is this possible?

Animation Clips are not “stored in” any object, they’re assets in your project. Any number of objects can have references to it. The same thing goes for the Animator Controller asset, which is what you’re editing in an Animator window. Multiple objects can use the same Animator Controller asset.

How best to design an Animator Controller for usability, reusability, extensibility, maintainability is a really deep topic you can’t just learn in one forum thread, either. You could take days, weeks, months to come up with a robust system yourself. And there are ways to skip ALL of that and do EVERYTHING by script, but those can be even more complex if you aren’t deeply familiar with how this all works under the hood.

I suggest you look for a couple of template systems like the Unity Starter Assets (Third Person) to study and learn the system, or paid assets like the Malbers Animal Controller [which includes humans] for a robust extensible system that you can just use directly.

Hey Halley,

Thanks for your reply, but I guess what I was really asking was does the clip reference need to be attached to the controller at build, or can it play any clip it is passed (if that clip uses the character’s avatar, in this case).
I’ve actually spent the last seven months designing a pretty robust interaction system using Forward And Back Reaching Inverse Kinematics, which works fine for limb placement, but I need something more granular for some objects, hence falling back to animation. I don’t want a load of edge cases cluttering up the FSM, and I don’t want to have to edit the character every time I add a new interactable object, so I have a system where the interactable actually controls the interaction. I can simply build a new object and drop it in the world. It’s infinitely extensible and far easier to debug. If that object could hold a reference to the animation required, it would save me a lot of work.

If I can use around 75% of an asset’s functionality out of the box then I will, especially if it doesn’t directly effect gameplay (so weather systems for example). If however I have to rework a large enough chunk of the code to make it fit for purpose (so most First Person Controllers on the market) then I’d rather write it myself, from scratch. It’s not really reinventing the wheel, as I’ve built up a fairly sizeable ‘toolkit’ over the last thirty or so years, but when I was writing MC68k Assembler for the Atari back in the mid 90’s 3D animation was a lot different to how it is now… ;o)

Any and all assets that are referenced by any serialized field in any component on any gameobject saved in a scene will be included in the build as long as that scene is included in that build. So yes, just make a public or serialized field for AnimationClip and set it to refer to the clip asset. It’s up to your code to use that field as you see fit.

Yes, you can play any arbitrary animation clip using AnimationPlayableUtilities.PlayClip(). Don’t forget to destroy the output playable graph when your animated gameObject is disabled/destroyed.

2 Likes

Thanks @tsukimi , exactly what I was after…