Unityにて設定した表情を、任意のキーボード入力によって切り替える処理について皆さまの知見からご教示賜りたく質問させていただきました。
以下、詳細です。
開発環境
・Unity 2018.2.14f1
・Visual Studio 2017
実装案
以下の2つの案を出して、「①」で実装する方向で決定しました。
①モデリングで作成したBlendShapeを任意のキーボード入力で切り替える。
②作成したAnimationを任意のキーボード入力で切り替える。
Unityにて設定した内容
・SkinnedMeshRendererを対象のモデルに設定
・上記モデルに対してスクリプト作成(後述します)
実装したスクリプト
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class blenderShapeControll : MonoBehaviour { 6 public GameObject head; 7 public SkinnedMeshRenderer facialExpression; 8 public float weight; 9 public int angry; 10 public int sad; 11 12 13 // Use this for initialization 14 void Start() { 15 angry = facialExpression.sharedMesh.GetBlendShapeIndex("eye_angry"); 16 head = GameObject.Find("head"); 17 facialExpression = GetComponent<SkinnedMeshRenderer>(); 18 19 } 20 21 // Update is called once per frame 22 void Update() { 23 if (Input.GetKey(KeyCode.A)) 24 { 25 if(facialExpression.GetBlendShapeWeight(angry) <= 0) 26 { 27 weight = 100; 28 facialExpression.SetBlendShapeWeight(angry, weight); 29 } 30 } else { 31 weight = 0; 32 facialExpression.SetBlendShapeWeight(angry, weight); 33 } 34 } 35 36 }
実行結果
以下のエラーが出力されました。
Array index (-1) is out of bounds (size=17) UnityEngine.SkinnedMeshRenderer:SetBlendShapeWeight(Int32, Single) blenderShapeControll:Update() (at Assets/Scripts/blenderShapeControll.cs:32)
「angry」に入っているインデックスの値が「-1」で入っているために起こったエラーと認識しました。
上記以外に、適切な実装方法や作成方法についてのページについて皆さまの知見からご教示賜りさく思います。
不足している情報などありましたら、お知らせください。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/03 09:02 編集