Unity IK(反向运动学)初探

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 : MonoBehaviour {

	protected Animator animator;
	public bool isActive = false;
	public Transform rightHandObj = null;
	private 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

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值