まゆたまガジェット開発逆引き辞典

電子工作やプログラミングのHowtoを逆引き形式で掲載しています。作りたいモノを決めて学んでいくスタイル。プログラマではないので、コードの汚さはお許しを。参照していないものに関しては、コピペ改変まったく問いません

VRMキャラクターの表情を一定時間ランダムに変える

こちら2つの応用編です。コルーチン使用部分はたぶん力技。
prince9.hatenablog.com
prince9.hatenablog.com


マイクの最大音量を計測し、そのときにランダムに表情を変えるというスクリプトを書いてる途中のメモです。
キーボードを押すごとにランダムに表情が変わり、2秒後に元に戻ります。

準備

こちらを参考にして、必要な表情を作成しておきます。
prince9.hatenablog.com

スクリプト

「BlendList = new List(){"Neutral","Smile","Setunai","SHINKEN"};」で使いたい表情をリスト化します。

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

public class FaceKeep : MonoBehaviour {
private VRMBlendShapeProxy proxy;


public List<string> BlendList;
private string rBlend;

//「処理を止める処理」を停止するか否か
bool isRunning = false;

	// Use this for initialization
	void Start () {
//使いたい表情のリスト
		BlendList = new List<string>(){"Neutral","Smile","Setunai","SHINKEN"};
		
	}
	
	// Update is called once per frame
	void Update () {
		

if (proxy == null)
        {
            proxy = GetComponent<VRMBlendShapeProxy>();
        }
        else
        {
			//上矢印キーをキーコードで判断
	if(Input.GetKeyDown(KeyCode.UpArrow)) {

//ランダムにリストから表情を選ぶ
		rBlend = BlendList[ Random.Range(0, BlendList.Count) ];
           Debug.Log(rBlend);

		proxy.SetValue(rBlend, 1.0f);

//時を止める処理
		StartCoroutine(TimeStop());

	} else {
			proxy.SetValue(BlendList[0], 1.0f);





	}
			


		}
		
	}

	IEnumerator TimeStop() {  
		//「処理を止める処理」を開始する
	if( isRunning ) { yield break; }
	isRunning = true;

//2秒時を止める
		yield return new WaitForSeconds(2.0f);

//2秒後表情を通常に戻す
		proxy.SetValue(rBlend, 0f);
				proxy.SetValue(BlendList[0], 1.0f);

				//「処理を止める処理」を停止する(動き出す)
isRunning = false;

	}
}