zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Unity Mecanim动画系统 之 动画混合树(Blend Trees)的简单使用

动画系统 简单 Unity 混合 Trees 使用
2023-09-11 14:20:50 时间

Unity Mecanim动画系统 之 动画混合树(Blend Trees)的简单使用

 

目录

Unity Mecanim动画系统 之 动画混合树(Blend Trees)的简单使用

一、简单介绍

二、官网的 Blend Trees 的解释

1)Blend Trees

2)1D Blending

3)2D Blending

三、注意事项

四、效果预览

五、实现步骤

六、关键代码


 

一、简单介绍

Unity中的一些基础知识点。便于后期开发使用。

Unity动画系统,也称为“Mecanim”,提供了以下功能:

  •         简单的工作流程,设置动画的所有元素,包括对象,角色和属性。

  •         支持导入外部创建的动画片段和使用内置动画编辑器制作的动画片段。

  •         人型动画重新定位,动画角色的运动控制可以被所有的角色模型共享,即角色的外观(SkinedMesh)和运动(Animator)是分离的,它们互相组合之后形成最终的动画。

  •         用于编辑动画状态的的简化工作流程,即动画控制器。

  •         方便预览动画片段,以及片段之间的插值过渡。 这使得动画师可以独立于程序员工作,在不运行游戏的情况下,可以对原型和预览动画进行预览。

  •         管理动画与可视化编程工具之间的复杂交互。

  •         不同的身体部位可以使用不同的动画逻辑控制。

  •         动画的分层和掩蔽功能。

状态机之中的状态不仅可以是单个剪辑,也可以是一个混合树。构建和编辑复杂的状态机和混合树,以便完全控制的角色如何运动。Unity编辑器提供强大的工具,用于分割、创建循环和从导入的动画文件中提取轨迹。然后可以把这些动画短片用作一个多层混合树的叶子,或者作为分层状态机中的一种状态。混合树让您只使用几个动画剪辑就能创建各种各样的运动。在混合树编辑器中,您可以定义混合参数并在3D视图中预览混合动画。混合树和动画剪辑一样,可以用作分层状态机中的状态。

动画混合树,就是多个动画切换更自然和谐,并且使得脚本控制更加容易,本节简单的介绍,混合树 Blend Trees 的使用。

 

二、官网的 Blend Trees 的解释

1)Blend Trees

A common task in game animation is to blend between two or more similar motions. Perhaps the best known example is the blending of walking and running animations according to the character’s speed. Another example is a character leaning to the left or right as it turns during a run.

It is important to distinguish between Transitions and Blend Trees. While both are used for creating smooth animation, they are used for different kinds of situations.

 

2)1D Blending

The first option in the Inspector
 of a Blend Node
 is the Blend Type. This drop-down is used to select one of the different blend types that can blend according to one or two parameters. 1D Blending blends the child motions according to a single parameter.

After setting the Blend Type, the first thing you need is to select the Animation Parameter that will control this Blend Tree. In this example, the parameter is direction which varies between –1.0 (left) and +1.0 (right), with 0.0 denoting a straight run without leaning.

 

3)2D Blending

The first option in the Inspector of a Blend Node is the Blend Type. This drop-down is used to select one of the different blend types that can blend according to one or two parameters. The 2D blending types blends the child motions according to two parameters.

The different 2D Blend Types have different uses that they are suitable for. They differ in how the influence of each motion is calculated.

(1)2D Simple Directional: Best used when your motions represent different directions, such as “walk forward”, “walk backward”, “walk left”, and “walk right”, or “aim up”, “aim down”, “aim left”, and “aim right”. Optionally a single motion at position (0, 0) can be included, such as “idle” or “aim straight”. In the Simple Directional type there should not be multiple motions in the same direction, such as “walk forward” and “run forward”.

(2)2D Freeform Directional: This blend type is also used when your motions represent different directions, however you can have multiple motions in the same direction, for example “walk forward” and “run forward”. In the Freeform Directional type the set of motions should always include a single motion at position (0, 0), such as “idle”.

(3)2D Freeform Cartesian: Best used when your motions do not represent different directions. With Freeform Cartesian your X parameter and Y parameter can represent different concepts, such as angular speed and linear speed. An example would be motions such as “walk forward no turn”, “run forward no turn”, “walk forward turn right”, “run forward turn right” etc.

 

三、注意事项

1)此Animator组件中的Apply Root Motion选项如果我们勾选了的话,当播放动画时是通过动画运动的幅度来改变角色的Transform的,如果我们不勾选,我们就可以用脚本设定此角色的Tranform。

 

四、效果预览

 

五、实现步骤

1、导入一个带动画的模型

 

2、新建 Animator Controller ,在控制器添加一个 Blend Tree

 

3、双击Blend Tree,进去编辑设置

 

4、点击 Blend Tree,设置 Blend Type

 

5、添加自己要控制的 Parameters (在 Animaor Controller 中构建的参数)

 

6、添加自己需要控制的动画

 

7、因为我们这里是要控制人物向前走,跑,左转右转,所以首先 Compute Positions 选择 Velocity XZ

 

8、然后,参考动画 左转右转 使用 角度控制更合适

 

9、返回 Blend Tree ,在 Compute Position 选择 X Position From 的 Angular Speed(Deg)

 

10、再根据需要,合理调整一下一些参数数值

 

11、退出 Blend Tree 编辑

 

12、新建一个脚本,来控 Animator Controller 的参数,来控制动画切换

 

13、把脚本,挂载到场景模型上

 

14、运行效果,如下

 

15、动画参数,混合树数量,根据自己需要调整

 

六、关键代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    // 动画控制机
    private Animator anim;

    // 参数 转为 StringToHash ID
    private int speedForwardId = Animator.StringToHash("SpeedForward");
    private int turnLeftRightId = Animator.StringToHash("TurnLeftRight");

    // Start is called before the first frame update
    void Start()
    {
        // 获取 动画控制机
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (anim !=null) {
            // 根据混合数的数值上下限设置后面的 乘以比例
            anim.SetFloat(speedForwardId,Input.GetAxis("Vertical")*4.15f);
            anim.SetFloat(turnLeftRightId, Input.GetAxis("Horizontal")*122.8f);
           
        }
        
    }
}