LoginSignup
4
4

More than 5 years have passed since last update.

モデル移動してもマウスドラッグの顔追従させる

Last updated at Posted at 2016-04-02

Live2D SDKサンプルのDemoプロジェクトで、Live2Dモデルの位置を中心以外に移動したらマウスドラッグの顔追従がずれてしまった。

<Live2Dモデルの位置が画面中心の場合はOK>
000.gif
<Live2Dモデルの位置が画面中心でない場合は顔の向きがずれる>
001.gif
SampleApp1プロジェクトには、モデルを移動しても顔追従できるけど行列などプログラムが複雑で困った...。
SampleApp1や難しいコードを使わず、簡単に直す方法を見つけたのでメモしておきます。

開発環境

・Live2D Unity SDK 2.1.00_1のDemoプロジェクト
・Unity5.3.1

修正箇所

ポイントとなる部分は以下のマウス位置の部分のみです。

SimpleModel.csの62行目あたり
// マウス左ボタンが押され続けている場合、マウス位置XとYをドラッグマネージャーに渡す
dragMgr.Set(pos.x / Screen.width * 2 - 1, pos.y / Screen.height * 2 - 1);

pos.xyは、画面左下が(X:0, Y:0)で右上が(X:画面width, Y:画面height)です。
002.png

pos.x / Screen.width * 2でマウス座標を0.0〜2.0の間に正規化します。
さらに-1する事で(X:0, Y:0)は(X:-1.0, Y:-1.0)、(X:2.0, Y:2.0)は(X:1.0, Y:1.0)になります。
003.png

これを以下のように直すと、モデル位置を変更しても追従します
(transformはLive2Dゲームオブジェクト)

SimpleModel.csの62行目あたり
dragMgr.Set(
    (pos.x  / Screen.width * 2-1) - transform.position.x,
    (pos.y  / Screen.height * 2-1) - transform.position.y
);

また、カメラの位置を変えた時にも対応するための最終コードは以下の通りです。
(カメラがOrthographicの時のみ)

SimpleModel.csの62行目あたり
// mainCameraにはMainCameraのゲームオブジェクトを取得しておく
dragMgr.Set(
    (pos.x  / Screen.width * 2-1) - (transform.position.x - mainCamera.transform.position.x),
    (pos.y  / Screen.height * 2-1) - (transform.position.y - mainCamera.transform.position.y)
);

004.gif
簡単にやるには上記方法が良いかと思います。
ちなみにSampleApp1から同じ機能を流用しようとしたら、以下のスクリプトが必要っぽい。

<マウスドラッグ用のクラス>
Assets/Scripts/sample/LAppView.cs
 → LAppLive2DManagerはコメント

<LAppViewで使うクラス>
Assets/Scripts/sample/LAppModel.cs
 → LAppModelProxyはコメント
 → ModelSettingはコメント

Assets/Scripts/sample/LAppDefine.cs
Assets/Scripts/sample/L2DModelMatrix.cs
Assets/Live2D/framework/L2DMatrix44.cs
Assets/Live2D/framework/L2DTargetPoint.cs
Assets/Scripts/utils/AccelHelper.cs
Assets/Scripts/utils/TouchManager.cs

4
4
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
4
4