Unity人形动画反向动力学IK动画实现

什么是反向动力学

以手掌移动为例子

正向动力学

        这个“力”是由你的身体躯干发出的,改变你的手臂位置,带动你的手掌位置移动

反向动力学

        这个"力"是直接在你的手掌上,直接改变你手掌的位置,并且通过手臂进而带动整个身体,“力”传递的方向是相反的所以被称为反向动力学。

实现过程

1、获取到动画组件animator,并且设置动画IK朝向的目标点

    private Animator animator;
    public GameObject target;
    private void Awake()
    {
        animator = GetComponent<Animator>();
        if(target == null )
        {
            target = new GameObject();
            target.name = "target";
        }
    }

2、所有的IK动画都是在OnAnimatorIK这个函数里面执行的,当你点击动画层级旁边的小齿轮会有IK Pass选项,勾选之后才会执行IK,当小齿轮左边有IK字样说明通带开启,开启之后IK代码才会生效。

3、IK动画实现代码,OnAnimatorIK是Unity自带的函数跟OnEnable一样。

 private void OnAnimatorIK(int layerIndex)
 {
     //AvatarIKGoal 这个是IK骨骼的枚举 
     //AvatarIKGoal.RightHand    右手
     //AvatarIKGoal.LeftHand     左手
     //AvatarIKGoal.RightFoot    右腿
     //AvatarIKGoal.LeftFoot     左腿

     //这个是控制肢体朝向点
     animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
     animator.SetIKPosition(AvatarIKGoal.RightHand,target.transform.position);
     //控制肢体的旋转
     animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
     animator.SetIKRotation(AvatarIKGoal.RightHand, Quaternion.Euler(0,90,0));

     //这个是控制动画头部的朝向点 就是看着的地方
     animator.SetLookAtWeight(1);
     animator.SetLookAtPosition(target.transform.position);
 }

小结

适合微调人形动画,让动画显示更加逼真

猜你喜欢

转载自blog.csdn.net/m0_74652911/article/details/132280372