前回の記事で、腕の制御まで作成できたので、今度は頭の制御を作成します。
以下のブログを参考にさせていただきました。
http://nn-hokuson.hatenablog.com/entry/2017/07/11/205245
視線追従用オブジェクトの設定
Unityちゃんの視線のターゲットとなるオブジェクトを追加します。
Camera(head)の子オブジェクトとして配置します。
配置場所はz = 5としました。
視線をオブジェクトに向ける
追加したオブジェクトに視線が向くようにします。前回の記事で作成したスクリプトを改修します。
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 lookTarget = 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 (lookTarget != null) { animator.SetLookAtWeight(1.0f, 0.3f, 1.0f, 0.0f, 0f); animator.SetLookAtPosition(lookTarget.position); } } }
新しく追加した「lookTarget」変数には、視線のターゲットオブジェクトを設定します。
以上!
これだけで、ヘッドマウントディスプレイの動きに合わせて、頭が動くようになります。
というわけで、頭の制御も意外と簡単にできます。
あとはリップシンクだけ組み込めば、VTuberとして最低限の機能は実装できるのではないでしょうか?
記事内のスクリーンショットはユニティちゃんライセンス条項の元に提供されています