天天看点

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