Blend an animation clip with a controller
Learn how to use a AnimatorMixerPlayable to blend an animation clip with an Animator Controller.
읽는 시간 3분최근 업데이트: 12일 전
This example demonstrates how to use an to blend an with an . To do this, you must first wrap each asset in its corresponding playable:
AnimationMixerPlayableAnimationClipAnimatorController- Wrap the (
AnimationClip) in anclip(AnimationClipPlayable).clipPlayable - Wrap the (
RuntimeAnimatorController) in ancontroller(AnimatorControllerPlayable).controllerPlayable
The method dynamically adjusts the blend weight of each playable.
SetInputWeight()Prerequisites
To use the script in your project, your project must have the following:
BlendClipWithController- A GameObject, such as a cube or a capsule. You don't need to manually add an Animator component to this GameObject. The attribute adds this component if it's not present.
RequireComponent - An animation clip that animates the properties of the GameObject. For example, an animation that changes the position and rotation of a GameObject.
- An Animator Controller with at least one state that uses a different animation clip from the one that animates the GameObject. If you use the same animation clip, there's no difference when the PlayableGraph blends between the clip and controller.
Add and run the script
To use the script in your project, follow these steps:
BlendClipWithController-
Add a script component to your GameObject. Name the script fileand use the following code:
BlendClipWithController.csusing UnityEngine;using UnityEngine.Playables;using UnityEngine.Animations;[RequireComponent(typeof(Animator))]public class BlendClipWithController : MonoBehaviour{ public AnimationClip clip; public RuntimeAnimatorController controller; public float weight; PlayableGraph graph; AnimationMixerPlayable mixer; void Start() { // Create the graph, the mixer, and bind them to the Animator. graph = PlayableGraph.Create("BlendClipWithController"); mixer = AnimationMixerPlayable.Create(graph, 2); var output = AnimationPlayableOutput.Create(graph, "Animation", GetComponent<Animator>()); output.SetSourcePlayable(mixer); // Create playables and connect them to the mixer. var clipPlayable = AnimationClipPlayable.Create(graph, clip); var controllerPlayable = AnimatorControllerPlayable.Create(graph, controller); graph.Connect(clipPlayable, 0, mixer, 0); graph.Connect(controllerPlayable, 0, mixer, 1); // Play the Graph. graph.Play(); } void Update() { // Clamp the weight between 0 and 1. weight = Mathf.Clamp01(weight); // Adjust the weight of each mixer input. mixer.SetInputWeight(0, 1.0f-weight); mixer.SetInputWeight(1, weight); } void OnDisable() { // Destroy all Playables and Outputs created by the graph. graph.Destroy(); }} -
In the Script component, select the animation clip for input node 0 and the Animator Controller for input node 1.
-
Specify a weight for the Animator Controller in relation to the animation clip.The script adjusts the weight of input node 0 in relation to input node 1 so that the combined weights equal 1.0. For example, a weight of 0.4 sets the Animator Controller to 40% weight and the animation clip to 60% weight.
-
Select Play to switch the Editor to Play mode.
-
In the Script component, experiment with different weights.
-
If you have installed the PlayableGraph Visualizer package, selectto display the PlayableGraph.
BlendClipWithController

The PlayableGraph generated by the BlendClipWithController script