転職を繰り返したサラリーマンの多趣味ブログ

30才未経験でSEに転職した人の多趣味ブログ

【Unity】アニメーションとスクリプトを連動させる方法

1.Animatorウィンドウで、以下の状態を作り出しておく。
f:id:uuc1h:20190224164014p:plain
※左のパラメータは、Parametersタブ / +をクリックし、Triggerを選択。
2.New Stateからget@ImageMokugyoの矢印をクリックし、遷移するトリガーを「isGetScore」を設定する。
f:id:uuc1h:20190224164223p:plain
※Has Exit Timeをオフにすることで、直ちに遷移される。
3.スクリプトは以下の通り。

// オーブ入手
    public void GetOrb(int getScore)
    {
        audioSource.PlayOneShot(getScoreSE);

        // AnimatorStateInfoオブジェクトの取得
        AnimatorStateInfo stateInfo = imageMokugyo.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0);
        
        // get@ImageMokugyoが再生中だったら
        if(stateInfo.fullPathHash == Animator.StringToHash("Base Layer.get@ImageMokugyo"))
        {
            // 最初から再生
            imageMokugyo.GetComponent<Animator>().Play(stateInfo.fullPathHash, 0, 0.0f);

        } else
        {
            // isGetScoreをオンにする
            imageMokugyo.GetComponent<Animator>().SetTrigger("isGetScore");
        }

        if (score < nextScore)
        {
            score += getScore;

            if (score > nextScore)
            {
                score = nextScore;
            }

            TempleLevelUp();
            RefreshScoreText();
            imageTemple.GetComponent<TempleManager>().SetTempleScale(score, nextScore);

            if ((score == nextScore) && (templeLevel == MAX_LEVEL))
            {
                ClearEffect();
            }
        }
        currentOrb--;
    }