How to store player character animations within environment objects and manage/extend states

Up until now I have only found examples of creating a big player character state machine with all the required animations handled by multiple state machines, states, layers etc. All fine and good, if you’re doing a simple idle, run, shoot game. However, this setup becomes unmanageable when you want to include specific animations for special cases, e.g. interacting with the environment.

I’ve seen other tutorials show, that you can be smart about states, creating “Interact_High” and “Interact_Low” states, and then trigger the needed one from the interacted object. This is ok, but not really satisfying when you want many different special animations.

How could I arrange the player’s mecanim state machine in a way, that allows environment objects to modify it? Basically, I would like to store the “Pick up object from ground” and “Pull this lever” animations within the corresponding environment objects and then dynamically assign and blend the animation of my character. Ideally, this would also work with animator override controllers, e.g. the player has default animations and gets specific environment interactions; the orc has different default animations, but still receives the same environment interactions.

The benefits I’m looking for:

  • Keep the default animations on a humanoid character as simple as possible
  • Extend character animations for specific interactions without adding new states to the player at edit time. Instead, create a system which allows new animations to be played by only creating a new environment object.
  • Completely separate the associated data, when it is not needed anymore. If the player doesn’t have a lever to pull in a scene, I don’t want to load the lever-pulling-animation. When the player leaves a certain area, I want to dynamically unload a group of animations, which are no longer relevant.

I hope I understand what you are looking for correctly. You can accomplish this type of behavior using the Playable API. There is a good example that was posted by @pierrepaul here:

The example goes over doing things like using weapon props to store and subsequently change the animator controller used by the character while it is equipped. I don’t any reason why you could use this technique to switch the character to a different controller based on your situations and upon completion transition back to the previously used controller.

One thing to note is that the Playable API, I believe, is still considered experimental at the moment, so some parts might change.

EDIT: Just dug up this blog post as well: https://blogs.unity3d.com/2016/04/13/unleashing-animation-and-audio-2/
It demonstrates a super simple way to tell an animator to play a clip that isn’t in the animator

1 Like

Thanks! The new Playable API looks very much like what I want, I’ll try it out, even if it’s experimental.

I’d still be interested to know, if anybody has achieved this sort of thing with the current mecanim implementation.

1 Like