• 02、Mecanim之IK动画


    序言:IK动画全名是Inverse Kinematics 意思是逆向动力学,就是子骨骼节点带动父骨骼节点运动。

    一、适用范围:

    在Mecanim系统中,任何正确设置了Avatar的人形动画都支持IK功能。

    二、常用函数:

    1、SetIKPositionWeight

    2、SetIKRotationWeight

    3、SetIKPosition

    4、SetIkRotation

    5、SetLookAtPosition

    6、bodyPosition

    7、bodyRotation

     

    如下就用了Ik功能,可以根据移动方块来控制手臂的移动:

                        

    三、IK功能的打开:

    选中一个动画模型,其必须完成了正确的骨骼映射,具体的骨骼映射步骤可以参考上一篇,

    为其创建动画状态机,这里需要注意,在动画层窗口中选中IK Pass选项,一定要选择,否则无法正确使用IK功能。

                        

    四、代码的控制:

    这里我们就可以通过以下代码进行控制角色的右手臂被方块控制移动了。

     1 using UnityEngine;
     2 using System;
     3 using System.Collections;
     4 
     5 [RequireComponent(typeof(Animator))]
     6 
     7 public class IK : MonoBehaviour
     8 {
     9 
    10     protected Animator animator;         //动画控制   
    11     public bool ikActive = false;           //是否开始IK动画   
    12     public Transform rightHand = null;        //右手子节点参考的目标
    13 
    14     void Start()
    15     {
    16        
    17         animator = GetComponent<Animator>();         //得到动画控制对象
    18     }
    19     //它是回调访法,必须勾选IK Pass!!!
    20     void OnAnimatorIK()
    21     {
    22         if (animator)
    23         {
    24 
    25             //即或IK动画后开始让右手节点寻找参考目标。 
    26             if (ikActive)
    27             {
    28 
    29                 //设置骨骼的权重,1表示完整的骨骼,如果是0.5,哪么骨骼权重就是一半,可移动或旋转的就是一半
    30                 animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1f);
    31                 animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1f);
    32 
    33                 //set the position and the rotation of the right hand where the external object is
    34                 if (rightHandObj != null)
    35                 {
    36                     //设置右手根据目标点而旋转移动父骨骼节点
    37                     animator.SetIKPosition(AvatarIKGoal.RightHand, rightHand.position);
    38                     animator.SetIKRotation(AvatarIKGoal.RightHand, rightHand.rotation);
    39                 }
    40 
    41             }
    42 
    43 
    44             //如果取消IK动画,哪么重置骨骼的坐标。
    45             else
    46             {
    47                 animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);
    48                 animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 0);
    49             }
    50         }
    51     }
    52 }

    五、总结:

          我这里只练习了右手臂的,IK定义可以控制两只手和两只脚的移动,所以大家可以去尝试尝试。

                                                                                                2017-12-17、11:31:43

  • 相关阅读:
    gcd
    Kuglarz
    三分题解
    杜教筛
    第一组dp解题报告
    dp总结1
    cf-BitwiseXor
    6.6总结
    图论总结
    CF1309总结
  • 原文地址:https://www.cnblogs.com/zhh19981104/p/8051626.html
Copyright © 2020-2023  润新知