LoginSignup
7
11

More than 5 years have passed since last update.

Unityちゃんの見る方向をIKで変える方法

Posted at

Unityちゃんの見る方向をIKで変える方法

オブジェクトの見る方向を変えるには一般的にTransform.LookAtを使いますが、今回はIKでもっと自然に見る方向を変えていきます。

Unityちゃんにアニメーションを付ける

Hierarchy内のUnityちゃんにAnimatorコンポーネントを付け、ControllerにAnimatorを配置します。(今回はUnityChanActionCheckが入っています)
image.png

今、UnityChanActionCheckの中身(ダブルクリックで確認)はこの様になっています。これでUnityちゃんのアニメーション自体が実行されます。
image.png

UnityちゃんにIKをつける

Animatorの左側にあるBaseLayerに注目してください。そこに歯車ボタンがあります。
image.png
それを押してIKPassにチェックを入れてください。これでIKが使えるようになります。
image.png

スクリプトを作る

今回はUnityちゃんがカメラの方向に向くようにします。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class afternoonUnitychanMotion : MonoBehaviour {

    Animator animator;
    Vector3 LookPos;


    void Start () {
        //Animatorコンポーネントを取得
        this.animator = GetComponent<Animator> ();
        //Mainカメラの方向を見るようにする準備
        this.LookPos = Camera.main.transform.position;
        }


    private void OnAnimatorIK (int layerIndex)
    {
        //どの部位がどのくらい見るかを決める
        this.animator.SetLookAtWeight (1.0f, 0.0f, 1.0f, 0.0f, 0f);
        //どこを見るか(今回はカメラの位置)
        this.animator.SetLookAtPosition (this.LookPos);
    }
}

【引数の意味】
SetLookAtWeight(全体の重み,身体を動かす重み,頭を動かす重み,目を動かす重み,モーションの制限量)

このスクリプトをUnityちゃんにアタッチして完成です!お疲れ様でした。

image.png

7
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
11