Search Unity

Question Animator.SetBool also change my AnimatorControllerPlayable param in another graph

Discussion in 'Animation' started by StayHungry-StayFoolish, Jun 14, 2021.

  1. StayHungry-StayFoolish

    StayHungry-StayFoolish

    Joined:
    Aug 2, 2017
    Posts:
    4
    I created a PlayableGraph with an AnimatorControllerPlayable which runtimeAnimatorController is the same as animator's runtimeAnimatorController. I want to control the param in these two runtimeAnimatorController separately. However, when I call Animator.SetBool, I found that two runtimeAnimatorController's param changed. And When I call AnimatorControllerPlayable.setBool, only one runtimeAnimatorController's param changed. Can anyone teach me why?
    Code (CSharp):
    1. void Update()
    2. {
    3.     if (Input.GetKeyDown(KeyCode.C))
    4.     {
    5.         m_graph = PlayableGraph.Create(m_animator.gameObject.name + "_fade_" + m_animator.GetHashCode());
    6.         m_graph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
    7.         m_fadeOutput = AnimationPlayableOutput.Create(m_graph, "fade", m_animator);
    8.         m_fadePlayable = AnimatorControllerPlayable.Create(m_graph, m_animator.runtimeAnimatorController);
    9.         m_fadeOutput.SetSourcePlayable(m_fadePlayable, 0);
    10.         m_graph.Play();
    11.         m_fadeOutput.SetWeight(1f);
    12.     }
    13.     if (Input.GetKeyDown(KeyCode.D))
    14.     {
    15.         m_animator.SetBool("test", true);  // both runtimeAnimatorControllers param changed!
    16.     }
    17.     if (Input.GetKeyDown(KeyCode.R))
    18.     {
    19.         m_animator.SetBool("test", false); // both runtimeAnimatorControllers param changed!
    20.     }
    21.     if (Input.GetKeyDown(KeyCode.Y))
    22.     {
    23.         var a = (AnimatorControllerPlayable)m_fadePlayable;
    24.         a.SetBool("test", true);   // only one runtimeAnimatorController param changed!
    25.     }
    26.     if (Input.GetKeyDown(KeyCode.U))
    27.     {
    28.         var a = (AnimatorControllerPlayable)m_fadePlayable;
    29.         a.SetBool("test", false);  // only one runtimeAnimatorController param changed!
    30.     }
    31.     if (Input.GetKeyDown(KeyCode.L))
    32.     {
    33.         var a = (AnimatorControllerPlayable)m_fadePlayable;
    34.         Debug.LogError("test param: " + m_animator.GetBool("test") + "  " + a.GetBool("test"));
    35.     }
    36. }
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    The Animator's regular Controller field isn't really intended to be used at the same time as Playables so any behaviour you get is basically just down to luck. Though I a bit surprised by the behaviour you're seeing.

    Most likely, that would only be possible with the exact graph layout you currently have, so it might stop if you just create an empty Playable connected to the output and connect your controller playable to it instead.
     
  3. StayHungry-StayFoolish

    StayHungry-StayFoolish

    Joined:
    Aug 2, 2017
    Posts:
    4
    The reason I use them together is that I want to make a transition between two animator override controller, so I create a graph with AnimatorControllerPlayable and set the animationOutput's weight from 1 to 0 in update. And at the same time switch the animator's runtimeAnimatorController.

    Sorry, I didn't get your mean, would you please explain more?


    What't more, is there any problems if I set Params like this?
    Code (CSharp):
    1. var a = (AnimatorControllerPlayable)m_animator.playableGraph.GetRootPlayable(0);
    2. a.SetBool("key", false);
    I did a simple test and it didn’t seem to be a problem, but I was worried that there would be hidden dangers.
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Just use Playable.Create to make an empty playable, connect its input to the controller playable, and connect the output to the empty playable so that the graph goes Output -> Playable -> Controller instead of Output -> Controller.

    I have no idea if setting parameters in the main controller's playable graph like that would have any hidden dangers or performance costs. I don't use Animator Controllers at all because it's a terribly designed system.
     
  5. StayHungry-StayFoolish

    StayHungry-StayFoolish

    Joined:
    Aug 2, 2017
    Posts:
    4
    I tried it, but it seems that it didn't work.