リアルとバーチャルの間

気まぐれに書いてます。

【Unity】はじめてのUnity IK

どもども、カヤック閃光部あまのです。

UnityでIKを実装することが出来ました!!
Unityサイトのリファレンスがわからなさすぎたのでブログで説明します。
骨とかリグの説明はこちら

まず、FBXで書き出されたキャラクターを読み込みUnityから球オブジェクトを作成しました。
f:id:amano-kiyoyuki:20130222174500j:plain


C#で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<Animator>();
}
void OnAnimatorIK(int layerIndex)
{
animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1.0f);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1.0f);
animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);
}
}

キャラクターのInspectorから以下の画像のように設定する。
f:id:amano-kiyoyuki:20130222180135j:plain
Animation Type:Hurmanoid
AvatarDefintion:Create From This Moel

次に、Animator Controllerを作成
InspectorのAnimator/ControllerにAnimator Controllerをアサイン
f:id:amano-kiyoyuki:20130222180913p:plain

AnimatorからIK PASSにチェックを入れる。
これでOnAnimatorIKが実行されるようになります。
f:id:amano-kiyoyuki:20130222181206p:plain

球をIKCtrlにアサインします。
f:id:amano-kiyoyuki:20130222181542p:plain


完成した動画!!

はじめてのUnity IK from _perfect on Vimeo.