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. Dismiss Notice

Playables - Animations

Discussion in 'Scripting' started by mixas19941, Aug 22, 2019.

  1. mixas19941

    mixas19941

    Joined:
    Jan 17, 2018
    Posts:
    1
    I recently discovered the unity Playable Graph and seemed like the perfect solution to my problem. I have a character with an AnimationController and I want to dynamically override some animations using AnimationClips.

    I tried the following, as shown in the script below:
    - I'm creating a PlayableGraph and setting up 2 layers. On the first layer I'm connecting an AnimatorControllerPlayable and giving it my character AnimatorController. On the second layer I'm connecting an AnimationClipPlayable and giving it my AnimationClip.
    - I then create 2 AvatarMasks to define the body parts with I want animated by each layer.

    Expected Outcome:
    The AnimationControllerPlayable should be controlling the lower body of my avatar and the AnimationClipPlayable should be controlling the upper body.

    Actual Outcome:
    The AnimationControllerPlayable seems to be controlling the entire avatar, which leads me to believe that I'm doing something with my AvatarMasks.

    Any light you can shed on this issue would be greatly appreciated!
    The problem can be easily reproduced in a new unity 2019.1.6f project using the attached script.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using UnityEngine.Playables;
    4.  
    5. using UnityEngine.Animations;
    6.  
    7. [RequireComponent(typeof(Animator))]
    8.  
    9. public class TestPlayable : MonoBehaviour
    10. {
    11.     public AnimationClip clip;
    12.  
    13.     public RuntimeAnimatorController controller;
    14.  
    15.     PlayableGraph playableGraph;
    16.  
    17.     AnimationLayerMixerPlayable mixerPlayable;
    18.  
    19.     void Start()
    20.  
    21.     {
    22.  
    23.         // Creates the graph, the mixer and binds them to the Animator.
    24.  
    25.         playableGraph = PlayableGraph.Create("CharGraph");
    26.  
    27.         Transform lefthand = this.transform.Find("Root/Hips/Spine_01/Spine_02/Spine_03/Clavicle_L/Shoulder_L");
    28.         Transform righthand = this.transform.Find("Root/Hips/Spine_01/Spine_02/Spine_03/Clavicle_R/Shoulder_R");
    29.  
    30.         Transform leftLeg = this.transform.Find("Root/Hips/UpperLeg_L");
    31.         Transform rightLeg = this.transform.Find("Root/Hips/UpperLeg_R");
    32.  
    33.  
    34.         var mask = new AvatarMask();
    35.         mask.AddTransformPath(lefthand, true);
    36.         mask.AddTransformPath(righthand, true);
    37.  
    38.  
    39.         var mask2 = new AvatarMask();
    40.         mask.AddTransformPath(leftLeg, true);
    41.         mask.AddTransformPath(rightLeg, true);
    42.  
    43.  
    44.         var playableOutput = AnimationPlayableOutput.Create(playableGraph, "Animation", GetComponent<Animator>());
    45.  
    46.         mixerPlayable = AnimationLayerMixerPlayable.Create(playableGraph, 2);
    47.         playableOutput.SetSourcePlayable(mixerPlayable);
    48.  
    49.  
    50.         // Creates AnimationClipPlayable and connects them to the mixer.
    51.  
    52.         var clipPlayable = AnimationClipPlayable.Create(playableGraph, clip);
    53.  
    54.         var ctrlPlayable = AnimatorControllerPlayable.Create(playableGraph, controller);
    55.  
    56.         clipPlayable.Play();
    57.  
    58.         mixerPlayable.ConnectInput(0, clipPlayable, 0, 1.0f);
    59.         mixerPlayable.ConnectInput(1, ctrlPlayable, 0, 1.0f);
    60.  
    61.         mixerPlayable.SetInputWeight(0, 1.0f);
    62.  
    63.         mixerPlayable.SetInputWeight(1, 1.0f);
    64.  
    65.         mixerPlayable.SetLayerMaskFromAvatarMask(0, mask);
    66.         mixerPlayable.SetLayerMaskFromAvatarMask(1, mask2);
    67.  
    68.         // Plays the Graph.
    69.         playableGraph.Play();
    70.         GraphVisualizerClient.Show(playableGraph);
    71.     }
    72.  
    73.     void OnDisable()
    74.     {
    75.         // Destroys all Playables and Outputs created by the graph.
    76.         playableGraph.Destroy();
    77.  
    78.     }
    79.  
    80. }
    81.  
     

    Attached Files:

  2. Bogdan_Nikolayev

    Bogdan_Nikolayev

    Joined:
    Dec 29, 2020
    Posts:
    9
  3. sdf

    sdf

    Joined:
    Sep 6, 2012
    Posts:
    13
    add these:
    Code (CSharp):
    1.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.Head, false);
    2.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.LeftArm, true);
    3.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.RightArm, true);
    4.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.LeftHandIK, false);
    5.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.RightHandIK, true);
    6.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.LastBodyPart, false);
    7.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.LeftLeg, true);
    8.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.RightLeg, true);
    9.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.RightFootIK, false);
    10.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.LeftFootIK, false);
    11.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.Body, false);
    12.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.Root, false);
    13.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.RightFingers, true);
    14.                 mask.SetHumanoidBodyPartActive(AvatarMaskBodyPart.LeftFingers, true);
    modify
    Code (CSharp):
    1.         var mask2 = new AvatarMask();
    2.         mask2.AddTransformPath(leftLeg, true);
    3.         mask2.AddTransformPath(rightLeg, true);