Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How do I access child animations in OnStateMachineEnter?

Discussion in 'Animation' started by tikali23, Jan 26, 2018.

  1. tikali23

    tikali23

    Joined:
    Apr 2, 2016
    Posts:
    3
    Hi guys,

    I'm trying to set up an animation behavior that allows me to change the animation clips inside a sub-state machine when entered. Basically, I want to prevent duplicating the same sub-state machine with different clips.
    So, what do I do with the animator and stateMachinePathHash parameters that the OnStateMachineEnter method has to access all child AnimatorClipInfos?
     
  2. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    Did you find out how to do this, the docs are crap?
     
  3. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,485
    You can't do it at runtime. The only access you get to the clips at runtime is through the RuntimeAnimatorController.animationClips which is an array of all clips (not linked to any specific state) and Animator.GetCurrentAnimatorClipInfo which only works for the current state. But even then, both of those are read-only so you can't even set their clips anyway.

    The only way to change clips at runtime is to use an AnimatorOverrideController, but that still won't do what OP wants because all it does is substitute clips by name, you still need to set up all the states and give them clips in the first place.

    This sort of thing would be really easy with my Animancer plugin though (link in my signature). It's much more flexible and entirely code based so you have much more freedom in the way you structure things. For example, one of the demo scenes I'm working on for the next version shows some sprite animations. Every action has a different animation for each facing direction (up/down/left/right) so I just made a DirectionalAnimationSet class which inherits from ScriptableObject and exposes an AnimationClip field in the inspector for each direction. So I have DirectionalAnimationSets for idle, walking, running, attacking, etc. which all use the same structure with different clips.
     
    Ziplock9000 likes this.