前回の記事で視線追跡による頭のトラッキングを紹介しました。
今回は、HMDの回転を、そのまま適用する方法を試してみます。
いきなりスクリプト
IKController.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class IKController : MonoBehaviour { [Header("HandAnchors")] [SerializeField] private Transform rightHandAnchor = null; [SerializeField] private Transform leftHandAnchor = null; [Header("Head Tracking")] [SerializeField] private Transform hmd = null; private Animator animator; // Use this for initialization void Start() { animator = GetComponent<Animator>(); } void OnAnimatorIK() { // 右手コントロール if (rightHandAnchor != null) { animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1); animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1); animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandAnchor.localPosition); animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandAnchor.rotation); } // 左手コントロール if (leftHandAnchor != null) { animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1); animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1); animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandAnchor.localPosition); animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandAnchor.rotation); } // 頭の追従 if (hmd != null) { Vector3 hmdEuler = hmd.rotation.eulerAngles; Vector3 finalEuler = Vector3.zero; finalEuler.x = hmdEuler.y * -1; finalEuler.y = hmdEuler.z; finalEuler.z = hmdEuler.x * -1; animator.SetBoneLocalRotation(HumanBodyBones.Neck, Quaternion.Euler(finalEuler)); } } }
hmdという変数を新たに宣言し、プログラムの終盤でhmdの回転軸を、HumanoidBoneのNeckへ適用することで頭を動かしています。
ボーンの軸と、HMDの軸にずれがあったため、途中に補正する処理が入っています。
HMDを設定
スクリプトに宣言した「hmd」変数には、SteamVR CameraRig内のCamera(head)を割り当てます。
以上
これで、Unityちゃんの頭がぐぉんぐぉん周ります。
上下左右、そして傾き、自由自在!
これで、手と頭の動きが出来ました。
あとはリップシンクが出来れば、VTuberとしてデビューできそうですね。
記事内のスクリーンショットはユニティちゃんライセンス条項の元に提供されています