Unity IK(反向运动学)初探

http://blog.csdn.net/myarrow/article/details/44450199

1. 简介

    IK与FK对应,正向运动学就是根骨骼带动节点骨骼运动。而反向运动学就是反过来,由子节点带动父节点运动。
    

2. 实例

    对于Humanoid的动画,使用的方法很简单,在Animator窗口中,对于要使用IK的动画状态勾上Foot IK选项,在Base Layer中勾上IK Pass选项。然后在代码中实现OnAnimatorIK函数来控制IK。




   其实例代码如下所示(IKCtrl.cs):

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System;  
  4.   
  5. [RequireComponent(typeof(Animator))]  
  6.   
  7. public class IKCtrl : MonoBehaviour {  
  8.   
  9.     protected Animator animator;  
  10.     public bool isActive = false;  
  11.     public Transform rightHandObj = null;  
  12.     private bool isFirst = true;  
  13.   
  14.     // Use this for initialization  
  15.     void Start () {  
  16.         Debug.Log ("Start is being called");  
  17.         animator = GetComponent<Animator> ();  
  18.     }  
  19.   
  20.     // a callback for calc IK, when Mecanim run, Animator will call this callback  
  21.     void OnAnimatorIK(int layerIndex)   {  
  22.         Debug.Log ("OnAnimatorIK is being called");  
  23.         if (animator) {  
  24.             // if the IK is active, set the position and rotation directly to the goal  
  25.             if(isActive){  
  26.                 // weight =1.0 for the right hand means position and rotation will be at the IK goal(the place the character wants to grab)  
  27.                 animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1.0f);  
  28.                 animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1.0f);  
  29.   
  30.                 // set the position and the rotation of the rihgt hand where the external object is  
  31.                 if(rightHandObj != null){  
  32.                     Debug.Log ("Set Avatar's position and rotation");  
  33.                     animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);  
  34.                     animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);  
  35.                 }else{  
  36.                     animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);  
  37.                     animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0);  
  38.                 }  
  39.             }  
  40.         }  
  41.     }  
  42. }  

3. 动画制作要点

     3DS Max中使用IK的情景还是非常多的。比如一个小章鱼,每个脚上面绑上IK,然后就可以通过脚步移动控制整条腿的运动。如果不用IK的话操作起来很麻烦而且不自然。
      而FBX的格式里面也是有IK信息的。只是Unity过滤了相关的数据而已。美术在导出动画之前做这么的操作,然后动画就正常了。
      1) Set up the bone structure as you please.
      2) Create the animations you want, using FK and/or IK
      3) Select all bones and/or IK solvers
      4) Go to Motion->Trajectories and press Collapse. Unity makes a key filter, so the amount of keys you export is irrelevant
      5) "Export" or "Export selected" as newest FBX format
      6) Drop the FBX file into Assets as usual
      在3dmax中ctrl+A选中所有所有骨骼,在右侧的选项卡中选择Motion->Trajectories,如果已经选择好骨骼,Collapse按钮就可以正常点击,点击这个按钮,然后正常的导出动画。这样Unity中的动画表现就跟3dmax一致了。

     注意,使用Collapse功能会修改动画的帧(使帧间隔变得一样),这样我们很多动作播放的时候就会被改变,比如攻击动作会变得很慢,没有力度。美术应该在Collapse后再次修改动画(或者是在制作动画之前使用Collapse)以保证动画的正确性。


参考:http://blog.csdn.net/langresser_king/article/details/22725499

原文地址:https://www.cnblogs.com/nafio/p/9137373.html