• Unity3D之Mecanim动画系统学习笔记(七):IK(反向动力学)动画


    什么是IK?

    IK(Inverse Kinematics)即反向动力学,即可以使用场景中的各种物体来控制和影响角色身体部位的运动,一般来说骨骼动画都是传统的从父节点到子节点的带动方式(即正向动力学),而IK则倒过来,由骨骼子节点带动骨骼父节点,具体情况比如人物走路踩到了石头就需要由脚的子节点来带动全身骨骼做出踩到石头的响应。

    IK可以使人物和场景更加贴合,从而达到更加真实的游戏效果,如果大家玩过《波斯王子》或《刺客信条》系列,应该对主角的攀爬和飞檐走壁的能力印象深刻,这些都是应用了IK,使动画贴合到具体的场景中进行的表现。

    Unity3D本身已经带有了IK的功能(http://docs.unity3d.com/Manual/InverseKinematics.html),我们接下来就对IK进行一下简单的学习和使用。

    FinalIK

    该插件是对Unity本身的IK的优化和增强,可以模拟出更加真实的效果,有兴趣可以看一看。

    https://www.assetstore.unity3d.com/cn/#!/content/14290

    实例

    我们直接上手一个小例子来看看Unity3D中的IK应该如何使用,我们会创建一个场景,使人物的头部始终面向一个点,同时创建四个点控制人物的手和腿的移动。

    我们在场景中添加一个人物和5个小球,如下:

    根据Unity官方的文档给出的资料来看,首先必须在需要使用IK动画的Animator的层上开启“IK Pass”,如下图所示:

    只有开启了这个选项,系统才会调用IK相应的方法。

    下面我们为这个人物添加一个脚本,如下:

    复制代码
     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 public class TestIK : MonoBehaviour
     5 {
     6     public Transform lookAtTarget;
     7 
     8     public Transform leftHandTarget;
     9     public Transform rightHandTarget;
    10     public Transform leftFootTarget;
    11     public Transform rightFootTarget;
    12 
    13     private Animator _animator;
    14 
    15     void Start()
    16     {
    17         _animator = this.GetComponent<Animator>();
    18     }
    19     
    20     void OnAnimatorIK(int layerIndex)
    21     {
    22         if(_animator != null)
    23         {
    24             //仅仅是头部跟着变动
    25             _animator.SetLookAtWeight(1);
    26             //身体也会跟着转, 弧度变动更大
    27             //_animator.SetLookAtWeight(1, 1, 1, 1);
    28             if(lookAtTarget != null)
    29             {
    30                 _animator.SetLookAtPosition(lookAtTarget.position);
    31             }
    32 
    33             _animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
    34             _animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
    35             if(leftHandTarget != null)
    36             {
    37                 _animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandTarget.position);
    38                 _animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandTarget.rotation);
    39             }
    40 
    41             _animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
    42             _animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
    43             if(leftHandTarget != null)
    44             {
    45                 _animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandTarget.position);
    46                 _animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandTarget.rotation);
    47             }
    48             
    49             _animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1);
    50             _animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1);
    51             if(leftHandTarget != null)
    52             {
    53                 _animator.SetIKPosition(AvatarIKGoal.LeftFoot, leftFootTarget.position);
    54                 _animator.SetIKRotation(AvatarIKGoal.LeftFoot, leftFootTarget.rotation);
    55             }
    56             
    57             _animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1);
    58             _animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1);
    59             if(leftHandTarget != null)
    60             {
    61                 _animator.SetIKPosition(AvatarIKGoal.RightFoot, rightFootTarget.position);
    62                 _animator.SetIKRotation(AvatarIKGoal.RightFoot, rightFootTarget.rotation);
    63             }
    64         }
    65     }
    66 }
    复制代码

    需要注意的是,控制IK的脚本必须添加到OnAnimatorIK方法中才会生效,下面看下效果图:

  • 相关阅读:
    解决Qt5 Creator无法切换输入法(fcitx),不能录入汉字问题
    QProcess进程间双向通信
    DB2使用存储过程插入数据
    Qt自定义圆周动画(360 10.0 的模仿作者写的)
    经典重温:给微软上课的快乐车夫
    很劲爆!紫光赵伟国在北京微电子会议上12条惊人语录
    中芯国际董事长周子学:技术终有山顶,我们用时间换技术
    windows完全支持C++11的轻量级编译器(官网MinGW和非官方的MinGW-builds)
    VMwarevSphere 服务器虚拟化之二十九 桌面虚拟化之安装View副本服务器
    Qt判断和打开进程(windows端),运行,检测,中止
  • 原文地址:https://www.cnblogs.com/lancidie/p/7345147.html
Copyright © 2020-2023  润新知