poi’s tech blog

3D多人数同時接続型球体アクション成人向けゲーム開発のためのアイデア、ナレッジ

まばたき

生きてる感じがする。

f:id:poipoipoip:20190502143626g:plain

以下まばたきコード

0~3秒のランダム周期で目のテクスチャをフレーム毎に閉じた目→半目→開いた目に切り替えてるだけ。

マテリアルの取得の仕方が良くない気がする。 複数オブジェクト複数マテリアルあるからfor文回して目的の目マテリアルの名前一致で取る。

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

public class ExpressionCtrl : MonoBehaviour {

    public Texture[] image_face;

    private double timeElapsed;
    private double timeMax;
    private double timeOut;

    private int blinkCount;

    private Material eye;


    // Use this for initialization
    void Start() {
        timeMax = 3.0;
        blinkCount = 0;
        SetTimeOutSec();

        Renderer[] children = this.GetComponentsInChildren<Renderer>();
        for (int i = 0; i < children.Length; i++)
        {
            for (var j = 0; j < children[i].materials.Length; j++)
            {
                if (children[i].materials[j].name == "kirby_face1 (Instance)")
                {
                    eye = children[i].materials[j];
                }
            }
        }
    }

    void Update()
    {
        timeElapsed += Time.deltaTime;

        if (timeElapsed >= timeOut)
        {
            // Do anything
            Blink();
        }
    }

    void SetTimeOutSec()
    {
        System.Random r = new System.Random();
        timeOut = timeMax * r.NextDouble();
    }

    void Blink()
    {
        switch (blinkCount)
        {
            case 0:
                eye.mainTexture = image_face[2];
                blinkCount++;
                break;
            case 1:
                eye.mainTexture = image_face[1];
                blinkCount++;
                break;
            case 2:
                eye.mainTexture = image_face[0];
                blinkCount = 0;
                timeElapsed = 0.0f;
                SetTimeOutSec();
                break;
            default:
                blinkCount = 0;
                timeElapsed = 0.0f;
                SetTimeOutSec();
                break;
        }
    }
}

こっから更に状態持たなきゃいけないからすぐにミートボールスパゲッティになる予定