プロ生ちゃんの視線を操作する(Animator.SetLookAtWeight)
プロ生ちゃんの視線に関して、その2。
今回はIKの機能を使って瞳だけを動かしてみます。
以前Mecanim Example ScenesのIK.csを利用したIKの記事を書きましたが、このIK.csを参考に瞳だけを動かすスクリプトを作ってみます。onoty3d.hatenablog.com
前回のtransformのLookAtメソッドを利用した記事では、元の目の回転等で補正する必要がありましたが、こちらはその必要が無いので簡単です。
事前にRigの設定で、プロ生ちゃんの目をHeadのLeft/Right Eyeにセットしておきましょう。
今回はAnimatorのSetLookAtWeightメソッドを利用します。
Unity - スクリプティング API: Animator.SetLookAtWeight
引数のweightとeyesWeightを1、他を0にすることで、目だけを動かします。
ソース
using UnityEngine; namespace PronamaChan { public class LookTargetIK : MonoBehaviour { public bool IKActive = false; public Transform Target; private Animator _animator; // Use this for initialization private void Start() { this._animator = this.GetComponent<Animator>(); } private void OnAnimatorIK(int layerIndex) { if (this._animator == null) return; if (this.IKActive) { this._animator.SetLookAtWeight(1.0f, 0f, 0f, 1.0f, 0f); if (this.Target != null) { this._animator.SetLookAtPosition(this.Target.position); } } else { this._animator.SetLookAtWeight(0.0f); if (this.Target != null) { this.Target.position = this._animator.bodyPosition + this._animator.bodyRotation * new Vector3(0, 0.5f, 1); } } } } }
スクリプトをプロ生ちゃんモデルに適用したら、Targetに目で追いかけたい対象を指定してください。
実行後、IKActiveにチェックを入れることで、目がTargetを追いかけるようになります。
前回と同じく、このままだと常に瞳が対象を追いかけるので、一定範囲を超えたら白目になってしまいます。