Unity IK 反向运动学)初探

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

1. 简介

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

2. 实例

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




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

using UnityEngine;using System.Collections;using System;[RequireComponent(typeof(Animator))]public class IKCtrl : MonoBehaviourprotected Animator animator; public bool isActive = falsepublic Transform rightHandObj = nullprivate bool isFirst = true// Use this for initialization void Start () {  Debug.Log ("Start is being called");  animator = GetComponent<Animator> (); } // a callback for calc IK, when Mecanim run, Animator will call this callback void OnAnimatorIK(int layerIndex{  Debug.Log ("OnAnimatorIK is being called");  if (animator) {   // if the IK is active, set the position and rotation directly to the goal   if(isActive){    // weight =1.0 for the right hand means position and rotation will be at the IK goal(the place the character wants to grab)    animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1.0f);    animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1.0f);    // set the position and the rotation of the rihgt hand where the external object is    if(rightHandObj != null){     Debug.Log ("Set Avatar's position and rotation");     animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);     animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);    }else{     animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);     animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0);    }   }  } }}

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

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/hddghhfd/article/details/84099348
ik