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
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity是一种流行的游戏引擎,具有强大的逆运动学仿真功能。逆运动学是指根据末端效应器的位置和姿态,计算出机械臂或角色模型关节的角度,以实现特定的动作或位置。 在Unity中实现逆运动学仿真可以通过使用Unity的Animator组件和在关节上应用逆运动学脚本来完成。首先,我们需要设置骨骼网格和模型的层级结构,以便每个关节都能够正确地连接和移动。然后,我们可以使用Unity的动画编辑器来创建和编辑动作状态机,以控制角色的动作。 接下来,我们可以在脚本中编写逆运动学算法来计算出关节的角度。算法通常包括使用逆矩阵计算和插值来找到最佳的关节角度,以使末端效应器达到所需的位置和姿态。在计算出角度后,我们可以将其应用于关节,以使模型相应地移动和转动。 Unity还提供了一些内置的逆运动学函数和工具,如IK系统和IK分解器。IK系统可用于自动计算关节角度,以使模型的身体部分或其他物体与目标对齐。IK分解器则可以将复杂的逆运动学问题分解成简单的子问题,以加快计算速度和提高仿真效果。 总的来说,Unity中的逆运动学仿真提供了一种方便而强大的方式来模拟机械臂或角色模型的运动和动作。通过结合动画编辑器和逆运动学算法,我们可以实现逼真的角色动作和复杂的关节控制,从而为游戏开发和虚拟现实应用提供更好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值