Unity3D高級動畫(Animator)-動畫狀態機

動態系統種類:

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

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

 

 

Animator的使用:

 

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

Rig裏我們可以設置動畫類型,默認設爲Generic。如果是要使用Mecanim提供的動畫retargeting等功能,那就需要將動畫類型設爲Humanoid。要設置循環播放該動畫,勾選Animations裏Loop Time即可。

這裏要想動畫在運行後動,那就要將動畫預製物的屬性修改爲Humanoid 如下圖:

 

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

 

 

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

 

 

(4)打開Animator窗口(雙擊動畫控制器可以打開Animator窗口),將之前導入的模型相關動畫拖入窗口(將對應動畫片段拖入生成新節點,就可以編輯了)。

 

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

橙色節點:相當於舊動畫系統的默認片段(新建的第一個節點默認爲橙色,自動與 Entry 連接,成爲進入狀態機後的第一個狀態);

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

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

灰色節點:

設置默認片段

建立基礎連線

箭頭代表兩個動畫的過渡

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

取消death過渡到自己的選擇

 

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

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

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

 

案例:

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
        }
    }
}

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);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章