Unity character animation configuration

Preface

The configuration of character animation is an important part of unity. Today I will do the entry-level character animation configuration.

One, first import the model animation

1. After the character model is imported, we will find that there will be some animations and character skeletons:

As shown in the figure below:
Insert picture description here
2. Click on the model to configure the relevant properties of the model in the inspector panel:

Among them, the animation is Rig and Animation . For the Animation Type that Rig needs to choose, because we configure character animation, we choose Humanoid, and there are other options to choose

As shown:

Insert picture description here
The main functions of the Animation option panel are:

Insert picture description here

The most commonly used in Animation is Loop Time. After checking it, the animation can be played repeatedly. When we configure the continuous actions of walking and running, we can check it, and jumping can choose not to check it.

Insert picture description here

Second, the creation of animator controller

1. Create an animator controller

Just right-click in the resource (or stand-alone on the resource in the navigation bar) to create an animator controller. After naming it, click to open the Animator panel, and you can configure the animation and other operations:
Insert picture description here

2. Configure an animation

2.1. To configure the animation, you first need to create a state, as shown in the figure below, right-click on the blank space to create an empty state, and then you can see the properties on the Inspector panel, and you can perform detailed configuration:

Insert picture description here
2.2. Next is another element, connect the line of animation state transition, right-click the animation state, and the option to create line will appear. At the same time, there are four options to control in the plus sign under Paramerers in the upper left corner:

1. Float and int can use the number size to control the animation.
2. The bool type control can trigger the event continuously.
3. Trigger can only trigger the event once

·
Insert picture description here

3. Add animation to the characters

Select the character, add an Animator component to the character, and drag in the animator controller and bones just created,
Insert picture description here

Three, use the script to call the four switching states to control

First get the Animator in the script:

private Animator anim;
void Start()
{
    
    
	anim = player.GetComponent<Animator>(); //player为挂在animator组件的人物
}

Then use four control states to control the transition of animation in time :

anim.SetTrigger("one");  //one为Trigger的名字
anim.SetBool("New Bool",false);  //bool类型的控制
anim.SetInteger("demo", 12);    //int类型

Four, examples

Description: Click a series of buttons to control the playback of character animation

Animator Controller:
Insert picture description here
Scene:
Insert picture description here
Code:

using UnityEngine;

using UnityEngine.EventSystems;

using UnityEngine.UI;

public class Buttonone : MonoBehaviour
{
    
    
    //所有Button的父节点
    public Transform btnParent;
    //private Button[] btns;
    private Button[] btns;
    private Animator anim;
    //有Animator组件的人物
    public GameObject player;

    void Start()
    {
    
    


        anim = player.GetComponent<Animator>();
        //初始化数组长度
        btns = new Button[btnParent.childCount];
        //便利父节点下所有的按钮
        for (int i = 0; i < btns.Length; i++)
        {
    
    
            //赋值按钮
            btns[i] = btnParent.GetChild(i).GetComponent<Button>();
            //为按钮添加点击事件
            
            btns[i].onClick.AddListener(nClick1);


        }
    }

    /// <summary>
    /// 按钮点击事件
    /// </summary>
    private void nClick1()
    {
    
    
        //按下时 判断当前点击的按钮的名字
        string btnName = EventSystem.current.currentSelectedGameObject.GetComponent<Button>().name;

        if(btnName=="Button")
        {
    
    
            anim.SetTrigger("one");
        }
        if (btnName == "Button (1)")
        {
    
    
            anim.SetTrigger("two");
        }
        if (btnName == "Button (2)")
        {
    
    
            anim.SetTrigger("three");
        }
        if (btnName == "Button (3)")
        {
    
    
            anim.SetTrigger("four");
        }
        if (btnName == "Button (4)")
        {
    
    
            anim.SetTrigger("five");
        }
        if (btnName == "Button (5)")
        {
    
    
            anim.SetTrigger("six");
            
        }
    }
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/xinzhilinger/article/details/109224600