Unity3D研究院之代码使用IK动画(五十五)

本文介绍Unity中IK(反向动力学)动画的概念及其实现方法。重点讲解如何通过代码动态控制IK动画,包括设置动画控制器、使用API进行IK计算等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.xuanyusong.com/archives/2264


         IK动画全名是Inverse Kinematics 意思是反向动力学,就是子骨骼节点带动父骨骼节点运动。比如跳街舞的少年用手撑着身体在地上转圈,手就是子骨骼,胳膊身体就是它的父骨骼,这时运动手就需要带动胳膊身体来移动。

 IK动画可以在3DMAX 或者Maya中制作(不在本篇的讨论范围内),本篇我么说说在程序中如何动态调用IK动画。IK动画需要使用Unity4新版的动画系统,如果对新版的动画系统不是很了解的朋友建议看看上一篇。Unity3D研究院之在项目中使用Unity4新Mecanim动画(五十三)

          如下图所示,设置模型的骨骼并且给模型添加AnimatorController控制器。如果不会添加详细请看上一篇文章。

屏幕快照 2013-05-02 下午12.41.26

 

在Unity导航菜单栏中打开Window->Animator打开动画控制器窗口,在这里勾选IK Pass。

B2FB49DC-2145-42DF-8BC2-6E6DA663A31C

 

 

代码方面直接使用API中的,我懒得写了,我给大家详细的解释一下。

01 using UnityEngine;
02 using System;
03 using System.Collections;
04  
05 [RequireComponent(typeof(Animator))] 
06  
07 public class IKCtrl : MonoBehaviour {
08  
09     //动画控制
10     protected Animator animator;
11     //是否开始IK动画
12     public bool ikActive = false;
13     //右手子节点参考的目标
14     public Transform rightHandObj = null;
15  
16     void Start ()
17     {
18         //得到动画控制对象
19         animator = GetComponent<Animator>();
20     }
21  
22     //a callback for calculating IK
23     //它是回调访法。
24     //前提是在Unity导航菜单栏中打开Window->Animator打开动画控制器窗口,在这里必须勾选IK Pass!!!
25     void OnAnimatorIK()
26     {
27           if(animator)
28           {
29  
30             //if the IK is active, set the position and rotation directly to the goal.
31             //即或IK动画后开始让右手节点寻找参考目标。
32             if(ikActive)
33             {
34  
35                //weight = 1.0 for the right hand means position and rotation will be at the IK goal (the place the character wants to grab)
36                //设置骨骼的权重,1表示完整的骨骼,如果是0.5哪么骨骼权重就是一半呢,可移动或旋转的就是一半,大家可以修改一下参数试试。
37                animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1f);
38                animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1f);
39  
40                 //set the position and the rotation of the right hand where the external object is
41                 if(rightHandObj != null)
42                 {
43                     //设置右手根据目标点而旋转移动父骨骼节点
44                     animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
45                     animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);
46                 }                  
47  
48             }
49  
50             //if the IK is not active, set the position and rotation of the hand back to the original position
51             //如果取消IK动画,哪么重置骨骼的坐标。
52             else
53             {          
54                 animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);
55                 animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0);            
56             }
57         }
58     }    
59 }

 

         如下图所示,图中哪个小球就是子节点参考的目标。,在Scene视图中移动哪个小球,你会发现主角的右手开始IK动画。

屏幕快照 2013-05-02 下午12.55.39

 

 

上面代码中我们IK的是主角的右手,实际上IK动画支持两个手和两个脚。

1 //  AvatarIKGoal.RightHand 右手
2 //  AvatarIKGoal.LeftHand  左手
3 //  AvatarIKGoal.LeftFoot  左脚
4 //  AvatarIKGoal.RightFoot 右脚
5 animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
6 animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);

 

OK!快快用代码来动态的制作你的IK动画吧,欢迎一起讨论技术,如有疑问请留言给我哇咔咔~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值