LoginSignup
0
1

More than 5 years have passed since last update.

Unityでコンボ攻撃を作ってみた

Posted at

今回はUnityでコンボ攻撃を作ってみました。

PlayerController.cs
    [SerializeField] bool combo1enable;
    [SerializeField] bool combo2enable;
    [SerializeField] bool combocombo = true;
    [SerializeField] bool cancombo = false;
    [SerializeField] float combo1time;
    [SerializeField] float combo2time;
    [SerializeField] float combo3time;
 void Start () {
        animator = GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update()  {
  if (Input.GetButtonDown("Attack") && !cancombo && !combo1enable && !combo2enable)
        { cancombo = true; }



        if (  cancombo && !combo1enable && combocombo)
        {
            //入力したら一定期間入力を受け付け、入力があったらコンボ2へ移行、なかったらキャンセル
            //コンボ1
            animator.SetBool("combo1",true);
            Debug.Log("コンボ1");

           combo1time += Time.deltaTime;
            if (combo1time > 0.1 && combo1time < 1) {
                //入力受付期間
                if (Input.GetButtonDown("Attack")) {
                    combo1time = 0;

                    combo1enable = false ;
                    combo1enable = true;
                    combocombo = false;
                    //タイマーを初期化し、コンボ1をオフにして、コンボ2をTrueにする
                }
                 }
            if (combo1time > 1) {
                //入力されなかったときの処理
                StartCoroutine("cancombocorutine");
                //StartCoroutine("cancomboenable");
                 }
        }

        if(combo1enable)
        {
            Debug.Log("コンボ2");
            //コンボ2
            animator.SetBool("combo2", true);



            combo2time += Time.deltaTime;
            if (combo2time < 1 && combo2time > 0.1)
            {
                if (Input.GetButtonDown("Attack"))
                {
                    combo2time = 0;
                    combo2enable = true;
                    combo1enable = false;
                }
            }
                if(combo2time > 1)
                {
                    combo1enable = false;
                    StartCoroutine("cancombocorutine");

                }

        }

        if (combo2enable)
        {
            Debug.Log("コンボ3");
            //コンボ3
            animator.SetBool("combo3", true);
            combo3time += Time.deltaTime;
            if (combo3time > 1)
            {
                StartCoroutine("cancombocorutine");
                combo2enable = false;
            }
        }
    }

    IEnumerator cancombocorutine()
    {

        Debug.Log("こーるちん");
        cancombo = false;
        combocombo = false;
        combo1enable = false;
        combo2enable = false;
        AttackReset();
        yield return new WaitForSeconds(0.5f);
        combocombo = true;
    }

    void AttackReset()
    {
        animator.SetBool("combo1", false);
        animator.SetBool("combo2", false);
        animator.SetBool("combo3", false);
        combo1time = 0;
        combo2time = 0;
        combo3time = 0;
    }
}


ググってもいい感じのが見当たらなくて、うんうん考えながら書いてました。
色々名前がクソなのは目をつぶってほしいです。

もっとこう、パパっと実装できちゃうような書き方とかあったら教えてほしい...
終わりです。

こんな感じで動きます

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1