unity新动画系统之IK动画

国际惯例,先来一段说明。IK动画全称Inverse Kinematics,即反向动力学,牵一发而动全身的既视感。
代码如下:

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

public class IK_test : MonoBehaviour {

    Animator _animator;
    public Transform sphereHead,sphereLeftFoot,sphereLeftHand;
    bool isIK = true;
    // Use this for initialization
    void Start ()
    {
        _animator = transform.GetComponent<Animator>();
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            isIK = false;
        }
    }
    //启动IK动画的unity特定方法OnAnimatorIK(),IK Pass要勾选才行
    private void OnAnimatorIK()
    {
        if (isIK)
        {
            //设置头部转向的权重,取值范围0-1,转动的幅度与数值呈正比(只有头部转动哦)
            // _animator.SetLookAtWeight(0.2f);
            //头部带动身体转动
            _animator.SetLookAtWeight(1, 0.5f);
            //设置权重的代码必须有,不然模型不响应
            _animator.SetLookAtPosition(sphereHead.position);

            //左脚跟随目标物位移   AatarIKGoal枚举 还有rightFoot,leftHand,rightHand
            _animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1);
            _animator.SetIKPosition(AvatarIKGoal.LeftFoot, sphereLeftFoot.position);

            //左手跟随目标物旋转   AatarIKGoal枚举 还有rightFoot,leftHand,rightHand
            _animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
            _animator.SetIKRotation(AvatarIKGoal.LeftHand, sphereLeftHand.rotation);

        }
        else
        {
            //模型复位,回到模型最初的状态
            _animator.SetLookAtWeight(0);
            _animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot,0);
            _animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 0);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/fenglele_fans/article/details/80540798