一.动画状态机的使用
1.动画状态机说明
2.动画切换箭头的Inspector面板
3.动画的Inspector面板
二.动画状态机的使用和脚本控制
1.动画状态机的使用
2.动画状态机的控制脚本
在脚本中,获取Animator组件,这个组件可以通过SetXXX()方法设置对应参数的值,XXX为参数类型,方法形参为状态机中参数名和设置的参数值
public class BikerController : MonoBehaviour { public Animator anim; float h = 0; float v = 0; // Start is called before the first frame update void Start() { anim = GetComponent<Animator>(); } // Update is called once per frame void Update() { h = Input.GetAxis("Horizontal"); v = Input.GetAxis("Vertical"); anim.SetFloat("Horizontal", h);//设置float类型参数Horizontal值为获取的h值 anim.SetFloat("Vertical", v);//设置float类型参数Vertical值为获取的v值 if (h == 0 && v == 0)//设置bool参数IsIdle为true anim.SetBool("IsIdle", true); else//设置bool参数IsIdle为false anim.SetBool("IsIdle", false); } }
三.动画的混合树
对于一些复杂的动画状态,如上面的动画状态,动画的切换箭头会非常多,不便于管理动画,因此有必要将一些同类型的系列动画做成混合动画,如控制玩家运动(走、跑、转向等,一般跳跃动画不做成混合动画)状态的动画可以由一个混合动画管理。
创建混合树后双击进入混合树
1.1D混合动画
同样通过脚本设置参数的值来控制动画播放状态
2.2D混合动画
2D混合有3种混合方式,在官方文档中对混合方式有详细说明,2D Simple Directional在一个方向上不能有多个运动,2D Freeform Directional在一个方向上可以有多个运动,2D Freeform Cartesian最好在运动不表示不同方向时使用。
3.直接混合
四.Animator的遮罩和层级等
五.动画的位置匹配
1.角色身体的某个点和环境中的点匹配
public void MatchTarget (Vector3 matchPosition, Quaternion matchRotation, AvatarTarget targetBodyPart, MatchTargetWeightMask weightMask, float startNormalizedTime, float targetNormalizedTime= 1);
参数:匹配的位置,匹配的旋转四元数,匹配的身体部位,权重,开始匹配的时间,结束匹配的时间
2.角色和地面的位置匹配
使用CharactorController组件进行匹配,使角色始终踩在地面上
3.IK动画匹配
/// <summary> /// IK匹配,在每一帧都会调用这个函数 /// </summary> /// <param name="layerIndex"></param>当前的动画层级 private void OnAnimatorIK(int layerIndex) { if(layerIndex == 1) { //角色有四个位置可以设置IK,分别使左右手和左右脚 //设置角色左手位置为leftHand的位置(空物体) anim.SetIKPosition(AvatarIKGoal.LeftHand, leftHand.position); //设置角色左手旋转为leftHand的旋转(空物体) anim.SetIKRotation(AvatarIKGoal.LeftHand, leftHand.rotation); //设置角色左手IK位置的权重为1 anim.SetIKPositionWeight(AvatarIKGoal.LeftHand,1); //设置角色左手IK旋转的权重为1 anim.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1); } }