Unity动画系统之IK(反向动力学)动画

发表于2018-08-22
评论0 5.5k浏览
IK(反向动力学),可以使用场景中的各种物体来控制和影响角色身体部位的运动,让人物和场景更加贴合,从而达到更加真实的游戏效果,下面就给大家介绍下IK的学习和使用。

代码如下:
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);
        }
    }
}

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论