[Unity animation layering] Avatar Mask realizes the upper body swinging a knife and the lower body walking

running result:

First realize the movement of the upper body, and then add the animation of the lower body walking after pressing the space bar.

 1.

The top base layer we created is empty, so don't worry about it.

The two animation layers below each store a clip. Show has the action of swinging a sword , and run has the action of running .

2.

Create a Mask as follows:

 

Whichever part you click on will turn red, indicating that it is in a motionless state.

 3.

Give to ShowLayer layer

 4.

Create another mask and give it to the run layer. Only the lower body can move.

 

5.

Code part, mounted on the character

public class Ctl : MonoBehaviour
{
    Animator animator;
    int baseLayer;
    int runLayer;
    int showLayer;
    private void Awake()
    {
        animator = GetComponent<Animator>();
        baseLayer = animator.GetLayerIndex("base");
        showLayer = animator.GetLayerIndex("ShowLayer");
        runLayer = animator.GetLayerIndex("run");
    }
    void Start()
    {
       
        //显示上半身
        animator.SetLayerWeight(baseLayer, 0f);
      
        animator.SetLayerWeight(showLayer, 1f);
    }


    // Update is called once per frame
    void Update()
    {
        //按下空格键
        //显示下半身动,同时上面的上半身动画也在执行(因为上面的权重还是为1)
        if(Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetLayerWeight(runLayer, 1f);
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_74022070/article/details/131103820