天天看點

Unity3D進階動畫(Animator)-動畫狀态機

動态系統種類:

Animation動畫狀态機:是舊版的動畫狀态機

Animator動畫狀态機:是新版的動畫狀态機,其實就是由Animation組成的(這裡我們常用這個)

Animator的使用:

(1)從網上找的3D模型FBX檔案,包括了模型的動畫檔案,模型材質等,是一個完整的資源(自帶Animator)。這裡我們用的人形模型。建立unity工程,将模型檔案導到Assets目錄。選中模型,在Inspector窗體可見:

Unity3D進階動畫(Animator)-動畫狀态機

Rig裡我們可以設定動畫類型,預設設為Generic。如果是要使用Mecanim提供的動畫retargeting等功能,那就需要将動畫類型設為Humanoid。要設定循環播放該動畫,勾選Animations裡Loop Time即可。

這裡要想動畫在運作後動,那就要将動畫預制物的屬性修改為Humanoid 如下圖:

Unity3D進階動畫(Animator)-動畫狀态機

(2)準備好模型後,建立一個動畫控制器Animator Controller(如果是Legacy動畫模式,不需要建立動畫控制器。Legacy是4.0版本前的标準動畫模式,功能較弱,使用也簡單)。

Unity3D進階動畫(Animator)-動畫狀态機

(3)把模型拖到Scene裡,給模型Animator元件的Controller指定動畫控制器。如需使用腳本控制模型位置,取消Apply Root Motion選項。

Unity3D進階動畫(Animator)-動畫狀态機

(4)打開Animator視窗(輕按兩下動畫控制器可以打開Animator視窗),将之前導入的模型相關動畫拖入視窗(将對應卡通片段拖入生成新節點,就可以編輯了)。

Entry(綠色節點):表示當進入目前狀态機時的入口,該狀态連接配接的狀态會成為進入狀态機後的第一個狀态;

橙色節點:相當于舊動畫系統的預設片段(建立的第一個節點預設為橙色,自動與 Entry 連接配接,成為進入狀态機後的第一個狀态);

Any State:表示任意的狀态,其作用是其指向的狀态是在任意時刻都可以切換過去的狀态;

Exit:表示退出目前的狀态機,如果有任意狀态指向該出口,表示可以從指定狀态退出目前的狀态機;

灰色節點:

Unity3D進階動畫(Animator)-動畫狀态機

設定預設片段

Unity3D進階動畫(Animator)-動畫狀态機

建立基礎連線

Unity3D進階動畫(Animator)-動畫狀态機

箭頭代表兩個動畫的過渡

Unity3D進階動畫(Animator)-動畫狀态機

任何狀态都可以連線到death,甚至包括自己到自己

Unity3D進階動畫(Animator)-動畫狀态機

取消death過渡到自己的選擇

建立過渡條件,添加參數,有4種(用來友善通過代碼激活參數,實作動畫切換)

Unity3D進階動畫(Animator)-動畫狀态機
Unity3D進階動畫(Animator)-動畫狀态機

預設過渡條件:Has Exit Time,播放完目前動畫才會進行到下一動畫的過渡。當有條件時,取消勾選;無條件,預設勾選

Unity3D進階動畫(Animator)-動畫狀态機

最後給遊戲物體添加腳本,控制參數的激活

案例:

Unity3D進階動畫(Animator)-動畫狀态機
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class gangcancel : MonoBehaviour
{
    private Animator anim;
    // Start is called before the first frame update
    void Start()
    {
        anim = this.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.N))
        {
            anim.SetTrigger("New Trigger"); //由dance1轉到dance2
        }
        if (Input.GetKeyUp(KeyCode.M))
        {
            anim.SetTrigger("2");//由dance2轉到dance1
        }
    }
}
           
Unity3D進階動畫(Animator)-動畫狀态機
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class catcancel : MonoBehaviour
{
    private Animator anim;
    public int jumpPower = 200;
    public int speed = 2;
    private Rigidbody rig;

    private Transform t;

    // Start is called before the first frame update
    void Start()
    {
        anim = this.GetComponent<Animator>();
        rig = this.GetComponent<Rigidbody>();

        t = this.GetComponent<Transform>();  //擷取本身和所有子子物體

    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.Space))
        {
            anim.SetTrigger("jump");
            rig.AddForce(transform.up * jumpPower);

        }
        if (Input.GetKey(KeyCode.W)) //當按下W鍵時,向前移動
        {
            t.Translate(t.forward * Time.deltaTime * speed);
            anim.SetFloat("speed", 2);
        }
        if (Input.GetKeyUp(KeyCode.W)) 
        {
            anim.SetFloat("speed", 0);
        }

        if (Input.GetKey(KeyCode.S))//當按下S鍵時,向後移動
        {
            t.Translate(-t.forward * Time.deltaTime * speed);
            anim.SetFloat("speed", 2);
        }
        if (Input.GetKeyUp(KeyCode.S))
        {
            anim.SetFloat("speed", 0);
        }
    }
}