反向动力学 (Inverse Kinematics)(仅限专业版 (Pro))
大多数动画经过旋转调整骨架中关节的视点为预订值来制造。子关节的方位依据其父关节的旋转而改变,因而,关节链的端点由其所包含的单个关节的视点和相对方位决议。这种构成骨架的办法称为正向动力学。
因而,从相反的视点检查构成关节的任务一般非常有用 –
在空间中给出一个挑选的方位,逆向操作,找到一种定向关节的有用方法,这样端点就落在该方位。想要人物在用户挑选的某一点接触一个物体或许将人物双脚稳稳地固定在不平坦的表面上时,这种方法非常有用。这种方法称为反向动力学
(Inverse Kinematics) (IK),撑持 Mecanim,可用于任何富含正确配置的 Avatar 的类人人物。
要为人物设置IK,一般需求具有与人物互动的场景附近的物体,然后经过脚本设置 IK,特别是经过此类动画器 (Animator) 功用:
SetIKPositionWeight、 SetIKRotationWeight、 SetIKPosition、
SetIKRotation、 SetLookAtPosition、 bodyPosition、 bodyRotation
在上图中,咱们展现了一个人物正捉住一个圆柱形物体。咱们如何才干做到这一点?
咱们先从一个具有有用 Avatar 的人物下手,然后向该人物附加一个实际上处理 IK 的脚本(咱们就称它为
IKCtrl):
using UnityEngine;
using System;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class IKCtrl :MonoBehaviour {
protected Animator animator;
public bool ikActive = false;
public Transform rightHandObj = null;
void Start ()
{
animator =
GetComponent();
}
//a callback for calculating IK
void OnAnimatorIK()
{
if(animator) {
//if the IK is active, set the position and rotation directly to
the goal.
if(ikActive)
{
//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 right hand where the
external object is
if(rightHandObj
!= null) {
animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);
}
}
//if the IK is not active, set the position and rotation of the
hand back to the original position
else
{ animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0); }
}
} }
因为咱们不计划让人物用手捉住整个物体,咱们在圆柱体上放置了一个手抓的球体,并相应滚动该球体。
该球体然后被列为 IKCtrl 脚本的“右手目标 (Right Hand Obj)” 特点