3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Unity]ヒットストップを作ってみる

Posted at

ヒットストップとは

ゲームで、攻撃がヒットした時に一瞬ゲームの流れが止まるようなあれです。スマブラのファルコンの膝や、モンハンの大剣などがイメージしやすいかなと。大技に多い印象です。

やってみる

前提

  • 敵と、プレイヤーに Box Collider が付与されている
  • 敵に Enemy というタグを設定する
  • プレイヤーの右足に RightFoot というタグを設定する
  • プレイヤーの攻撃モーションで、右足が敵に当たる

今回使ったもの

スクリプト

プレイヤーのスクリプトを作成し、以下の記述を付け加えます。
敵と接触した時に発火される OnCollisionEnter を使います。

    Animator animator;
    private string enemyTag = "Enemy";
    private int count = 0;
    private bool isHitStop = false;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        // ヒットストップ中ならば
        if (isHitStop) {
            count += 1;
            // カウントが20いったなら、ヒットストップを元に戻す
            if (count > 20) {
                isHitStop = false;
                animator.speed = 1;
            }
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        // 敵と接触したならば
        if (collision.collider.tag == enemyTag)
        {
            Debug.Log("敵と接触した!");
            animator.speed = 0.01f; // アニメーション速度を遅くし、ヒットストップを演出する
            isHitStop = true;
            count = 0;
        }
    }

これで、プレイヤーが敵と当たった時にヒットストップするようになります。
敵にも同じように判定を付けてあげれば、敵側も止まるようになります。

こんな感じ

movie_003_Trim.gif

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?