Unity3D之Mecanim动画系统学习笔记(九):Blend Tree(混合树)

来源:互联网 发布:java获取request对象 编辑:程序博客网 时间:2024/06/05 21:17

认识Blend Tree

我们在Animator Controller中除了可以创建一个State外还可以创建一个Blend Tree,如下:


那么我们看下新创建的Blend Tree和State有什么区别:


唯一的区别就是Montion指向的类型变成了Blend Tree类型,那么一个Blend Tree其实也就是一个状态,和状态不同的地方就是一个状态只能设定一个动画,而一个Blend Tree则可以设定为多个动画的混合。

混合树是Mecanim动画系统中比较复杂的一个内容,且其分为多个维度,下面我们逐个的来进行学习。

一维混合树

我们以官方的例子使用一维混合树来实现下面的效果:我们人物的跑动分为3个动画,分别是向前跑、向左跑和向右跑,其中向左跑和向右跑人物都会有一定的倾斜,这样更加符合现实的情况,那么我们在状态机中跑动只有一个状态,所以我们的跑动需要设置为混合树来混合这3个动画。

首先我们需要创建一个新的场景,拖入我们的人物模型,然后创建一个Animator Controller并对其进行配置:


注意我们的Run是Blend Tree而不是State,双击Run就可以进入混合树的编辑界面。

右击我们的混合树添加3个Motion,如下:


同时我们设定好3个方向的跑动动画:


我们还需要设定一个名为Direction的Float类型的参数来控制这个混合树:


接下来我们取消Automate Thresholds的选项,并按下图进行选择,系统会为我们配置好阀值:


现在我们点击预览框查看动画播放时就可以通过拖拽小红线来看不同的变化了,我们的可使用的角度范围为-130到130之间。

到现在我们的动画控制器就配置好了。

脚本

下面我们使用脚本来控制一下人物,我们给人物添加下面的脚本即可:

using UnityEngine;using System.Collections;public class TestBlendTree : MonoBehaviour{    public float DirectionDampTime = 30.0f;    private Animator _animator;    void Start()    {        _animator = this.GetComponent<Animator>();    }        void Update()    {        if(Input.GetKeyDown(KeyCode.W))        {            _animator.SetBool("run", true);        }        if(Input.GetKeyUp(KeyCode.W))        {            _animator.SetBool("run", false);        }        AnimatorStateInfo state = _animator.GetCurrentAnimatorStateInfo(0);        //奔跑状态下才允许转弯        if(state.shortNameHash == Animator.StringToHash("Run"))        {            //指定人物转弯通过控制混合数的参数即可            float h = Input.GetAxis("Horizontal") * 130.0f;            //DirectionDampTime 指示了每秒可以到达的最大值            //deltaTime 表示当前帧的时间            _animator.SetFloat("Direction", h, DirectionDampTime, Time.deltaTime);        }        else        {            //重置一下参数            _animator.SetFloat("Direction", 0);        }    }}

为了让摄像机跟随人物,我们直接添加官方给出的这个脚本到摄像机上即可:

using UnityEngine;using System.Collections;public class ThirdPersonCamera : MonoBehaviour{    public float distanceAway;            // distance from the back of the craft    public float distanceUp;            // distance above the craft    public float smooth;                // how smooth the camera movement is        private GameObject hovercraft;        // to store the hovercraft    private Vector3 targetPosition;        // the position the camera is trying to be in        Transform follow;        void Start(){        follow = GameObject.FindWithTag ("Player").transform;        }        void LateUpdate ()    {        // setting the target position to be the correct offset from the hovercraft        targetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;                // making a smooth transition between it's current position and the position it wants to be in        transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);                // make sure the camera is looking the right way!        transform.LookAt(follow);    }}

记得将人物的Tag设置为Player,同时脚本也要设置一下:


动画方面的一点要求

每个混合树的动画有一些要注意的地方:

  1. 动画长度需要一致;
  2. 动画的起始姿势需要一致;

二维混合树

同1维混合树,不过二维混合树已经作为一个平面来处理,同时需要两个参数来进行控制。对于更复杂的动画融合可以使用该模式,这里就不深入学习了。

我们可以将两个1维混合树合并为一个2维混合树来控制。

多维混合树

多维混合树在Unity5时添加,其配置更加复杂,一般使用在脸部表情的动画融合上。

天道酬勤,功不唐捐!

原文地址:http://www.cnblogs.com/hammerc/p/4832642.html

0 0