대부분의 캐릭터 애니메이션은 골격의 조인트 각도를 사전 지정한 값으로 회전하여 생성됩니다. 자식 조인트의 위치는 부모 조인트의 회전에 따라 달라집니다. 조인트 체인의 끝점은 체인에서 개별 조인트의 각도와 상대적인 위치에 따라 결정됩니다. 이러한 골격 배치 방식을 순운동학이라고 합니다.
하지만 반대 관점에서 조인트를 배치하는 것이 유용한 경우가 많습니다. 공간에서 선택한 위치 또는 목표에서 시작해 역으로 작업하여 끝점이 목표에 도달하도록 조인트의 방향을 설정하는 방법을 찾습니다. 캐릭터가 오브젝트를 잡거나 고르지 않은 표면에 서고 싶을 때 유용할 수 있습니다. 이러한 접근 방식을 역운동학(IK)이라고 합니다. 아바타가 올바르게 구성된 휴머노이드 캐릭터를 대상으로 메카님에서 지원됩니다.
캐릭터의 IK를 설정하려면 일반적으로 씬 주변에 캐릭터가 상호 작용하는 오브젝트가 있어야 합니다. 이러한 오브젝트와 캐릭터를 사용하여 스크립트 내에서 IK를 설정할 수 있습니다. 다음 애니메이터 함수를 사용할 수 있습니다.
예를 들어 위 이미지는 원통형 오브젝트를 터치하는 캐릭터를 보여 줍니다. IK와 스크립팅을 통해 이를 수행하려면 다음 단계를 따르십시오.
OnAnimatorIK 콜백을 전송합니다. 이후 단계에서는 이 콜백을 사용하여 스크립트에 역운동학을 구현합니다.

IKControl입니다. 이 스크립트는 캐릭터의 오른손에 대한 IK 타겟을 설정합니다. 또한 이 스크립트는 캐릭터가 실린더 오브젝트를 잡으면 해당 오브젝트를 바라보도록 위치를 변경합니다. 전체 스크립트는 아래와 같습니다.using UnityEngine;
using System;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class IKControl : MonoBehaviour {
protected Animator animator;
public bool ikActive = false;
public Transform rightHandObj = null;
public Transform lookObj = null;
void Start ()
{
animator = GetComponent<Animator>();
}
//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) {
// Set the look target position, if one has been assigned
if(lookObj != null) {
animator.SetLookAtWeight(1);
animator.SetLookAtPosition(lookObj.position);
}
// Set the right hand target position and rotation, if one has been assigned
if(rightHandObj != null) {
animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);
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 and head back to the original position
else {
animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0);
animator.SetLookAtWeight(0);
}
}
}
}
오른손이 실린더 게임 오브젝트를 통과하지 않도록 하려면 Cylinder 게임 오브젝트에 빈 자식 게임 오브젝트를 추가합니다. 이렇게 하려면 계층 창에서 실린더 게임 오브젝트를 오른쪽 클릭하고 Create Empty를 선택합니다. 이 빈 자식 게임 오브젝트의 이름을 Cylinder Grab Handle로 지정합니다.
오른손이 실린더를 통과하지 않고 터치할 수 있도록 Cylinder Grab Handle 게임 오브젝트를 배치하고 회전합니다.

Cylinder Grab Handle 게임 오브젝트를 IKControl 스크립트의 Right Hand Obj 프로퍼티로 할당합니다.Cylinder 게임 오브젝트를 Look Obj로 할당하여 IK Active가 활성화되면 캐릭터가 실린더의 중앙을 향하도록 합니다.
IK Active 체크박스를 활성화하고 비활성화하면 캐릭터가 실린더 게임 오브젝트를 터치하고 놓아야 합니다. 플레이 모드에서 Cylinder 게임 오브젝트의 위치와 회전을 변경하여 오른손과 캐릭터가 어떻게 반응하는지 확인합니다.