(この記事の使用環境: Unity5.4.2f2 Personal、Windows10


前回までの続き。

今回はジャンプモーションとジャンプ攻撃モーションを付けてみた。
引き続き Melee Axe Pack のモーション(とモデル)を使っていきます。

今回は苦労してます・・・。段階を追って説明していきます。

最初の状態はこんな感じ。
ジャンプモーションとジャンプ攻撃モーションは付けられましたが、モーション終了後に開始前の位置に戻ってきてしまっています。


まずはここまでの設定を説明していきます。
Animatorの遷移図はこんな感じ。
武器装備中ならジャンプ攻撃、武器をしまっている状態なら攻撃なしのジャンプをします。
BlogPaint

パラメータは"Jump"(Trigger)と、ジャンプを許可するタイミングを管理する"JumpChance"(Bool)、JumpAttackChance(Bool)を作りました。
このあたりの考え方は前回の記事で連続攻撃を実装したときとほぼ同じです。
全画面キャプチャ 20161231 144451-001

で、まずは攻撃なしの通常ジャンプから。
前に向かって走っているときのみ、スペースキーを押すとジャンプできるようにしています。

遷移条件。
全画面キャプチャ 20161231 144904
全画面キャプチャ 20161231 150834

で、前進走行アニメーションのところに、StateMachineBehaviour の"Jump"スクリプトを作成して付けています。
全画面キャプチャ 20161231 144859
全画面キャプチャ 20161231 151105

Jumpスクリプトの中身。前進走行中だけジャンプ操作を受け付けるようBool値を設定しています。

using UnityEngine;
using System.Collections;

public class Jump : StateMachineBehaviour {

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        animator.SetBool ("JumpChance", true);
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.SetBool ("JumpChance", false);
    }
}

で、今度はジャンプ攻撃モーションのほう。
遷移条件。
全画面キャプチャ 20161231 144756
全画面キャプチャ 20161231 151700

通常ジャンプと同様、前進走行アニメーションのところにBehaviorを付けています("JumpAttack")。
全画面キャプチャ 20161231 144845
全画面キャプチャ 20161231 151925

JumpAttackスクリプトの中身。Jumpスクリプトとほぼ同じ。

using UnityEngine;
using System.Collections;

public class JumpAttack : StateMachineBehaviour {

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.SetBool ("JumpAttackChance", true);
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.SetBool ("JumpAttackChance", false);
    }
}

で、プレイヤーを動かすスクリプトへの追記。ここはシンプルです。

    void FixedUpdate ()
    {
        if (Input.GetKeyDown (KeyCode.Space))
        {
            //  --- 武器なしで前に走っているときのみ有効 ----------------------------------------------------
            if (anim.GetBool("JumpChance") == true)      
            {
                anim.SetTrigger ("Jump");               // ジャンプする(アニメーション)
            }

            //  --- 武器ありで前に走っているときのみ有効 ----------------------------------------------------
            if (anim.GetBool ("JumpAttackChance") == true) 
            {
                anim.SetTrigger ("Jump");               // ジャンプする(アニメーション)
            }
        }

ここまでで冒頭の動画のように、ジャンプとジャンプ攻撃モーションを付けられましたが、モーション後に元の位置に戻ってきてしまう状態になります。
長くなってしまったので、それの修正については次の記事で書くことにします。

今回は以上です。