Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

¿Change animation controller state motion clip at runtime in Unity 5 ?

Discussion in 'Animation' started by Alima-Studios, Aug 2, 2015.

  1. Alima-Studios

    Alima-Studios

    Joined:
    Nov 12, 2014
    Posts:
    78
    It will be nice something like this

    Animator ac;
    AnimatorState as ;

    ac= GetComponent<Animator>();
    as =ac.GetStateByName("Base Layer\Run"); //this function does noes exist
    as.clip = newclip ;

    is there any way to do this ?

    thanks

     
    Last edited: Aug 2, 2015
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    sickshredzz likes this.
  3. Alima-Studios

    Alima-Studios

    Joined:
    Nov 12, 2014
    Posts:
    78
    I have try something like this too

    1. RuntimeAnimatorController myController = animator.runtimeAnimatorController;
    2. AnimatorOverrideController myOverrideController =newAnimatorOverrideController();
    3. myOverrideController.runtimeAnimatorController = myController;
    4. myOverrideController["run"]= myRunFasterClip;
    5. // Put this line at the end because when you assign a controller on an Animator, unity rebind all the animated properties
    6. animator.runtimeAnimatorController = myOverrideController;

    from http://answers.unity3d.com/questions/436360/reusing-an-animator-controller-for-generic-animati.html

    the idea is changing the clip for different ones some times... but it seems to work only the first one

    lot of thanks
     
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Just to let you know in 5.2 when you assign an overridecontroller the animator doesn't reset anymore the state machine memory
     
  5. Alima-Studios

    Alima-Studios

    Joined:
    Nov 12, 2014
    Posts:
    78
    The only way it works multiple times:

    RuntimeAnimatorController myController = Animator2.runtimeAnimatorController;
    AnimatorOverrideController myOverrideController = new AnimatorOverrideController();
    myOverrideController.runtimeAnimatorController = myController;
    AnimationClip nuevo = Instantiate(CurrentComandoItem.AnimacionParaBebeCuerpo);

    nuevo.name = "customanimation";
    myOverrideController["customanimation"] = nuevo;
    Animator2.runtimeAnimatorController = myOverrideController;
    Animator2.runtimeAnimatorController.name = "newname";

    Animator2.SetTrigger("PlayCustom");

    tryed on 5.2

    is there any other way ?
     
  6. DinostabOMG

    DinostabOMG

    Joined:
    Jan 4, 2014
    Posts:
    26
    Is there any alternative for doing this at runtime, Mechanim-Dev?
     
  7. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    This is still the only official way to change a clip at runtime.

    But we are working on a new interface that will allow you to play any clip at any time.
    http://docs.unity3d.com/ScriptReference/Experimental.Director.AnimationClipPlayable.html

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Experimental.Director;
    3.  
    4. AnimationClip myclip = ...;
    5. AnimationClipPlayable m_Playable = AnimationClipPlayable.Create(myclip);
    6. GetComponent<Animator>().Play(m_Playable);
    7.  
    Just don't forget to call m_Playable.Destroy(); when you don't need anymore the playable
     
    jeffersonrcgouveia likes this.
  8. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    151
    And how do I play that animation from another layer index?

    Play(m_Playable,1,1) doesn't work as it says it has to be a string.
     
  9. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,817
    Hi there,

    I'm trying to swap out a clip at runtime from an array of possible clips but I am struggling with this a bit.
    With the example code you are using .Play, is there a way to change the clip without playing it yet, I'd like to do that so I can blend to it from another state.
    Also I'm not sure how you tell mecahnim which state you want to swap clips on? In this example, how would I change the top blend clip specifically.

    And when you say to destroy the m_playable, do you mean after the clip is no longer in use?

    Thanks,
    Pete
     
  10. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,817
    Okay I'm looking at workarounds for this, does this sound okay? Or crazy...
    A blendtree with every clip I might want to replace and a float to control the current clip.



    Will that put a lot of stress on the CPU? I'll end up with about 20 clips.
    Thanks,
    Pete
     
  11. ENOSI_Studio

    ENOSI_Studio

    Joined:
    Sep 13, 2020
    Posts:
    5