LoginSignup
2
1

More than 5 years have passed since last update.

剣での攻撃と当たり判定の作成※パンチとかキックは判定を手や足につければできるよ♪

Last updated at Posted at 2017-11-16

剣の表示と位置調整
1.png

Createをクリック→Cubeを作成→刀身に重ねる
hantei.gif

判定を剣の子オブジェクトにする
2.png

剣を右手の子オブジェクトにする
3.png

剣で攻撃するアニメーションを追加
4.png

攻撃アニメーションの名前の変更
5.png

Attack変数の作成
6.png

アニメーションの遷移
7.png

待機アニメーション→攻撃アニメーションの遷移の設定
8.png

攻撃アニメーション→待機アニメーションの遷移の設定
9.png

攻撃アニメーションを終わらせるスクリプトの作成

attack.cs
using UnityEngine;
using System.Collections;

public class attack : StateMachineBehaviour
{

    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {

        animator.SetBool("Attack", false);
    }   
}

攻撃アニメーションを終わらせるスクリプトの貼り付け
10.png

以下のスクリプトはこの記事に書いたスクリプトに追記してます。面倒くさかったら丸ごとコピペして上書きしてね♪

Player.cs
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{

    float h, v;

    Animator anim;

    void Start()
    {

        anim = GetComponent<Animator>();
    }

    void Update()
    {


        h = Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");

        if (v != 0f || h != 0f)
            anim.SetBool("isRunning", true);
        else
            anim.SetBool("isRunning", false);
//ここから追加部分        
// z キーが押された時
        if (Input.GetKeyDown(KeyCode.Z))
        {
            anim.SetBool("Attack", true);
        }
//ここまで
    }
}

スクリプトを貼り付けるオブジェクト:thirdpersoncontrollerの下に入れた主人公

GetAnimationInfo.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GetAnimationInfo : MonoBehaviour
{

    public GameObject obj;
    private AnimatorStateInfo stateInfo;
    private Animator anim;

    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {

        stateInfo = anim.GetCurrentAnimatorStateInfo(0);
        if (stateInfo.IsName("Base Layer.attack"))
        {
            obj.SetActive(true);
        }
        else
        {
            obj.SetActive(false);
        }

    }
}

スクリプトを貼り付けるオブジェクト:剣の下に入れた判定

アニメーションが終わったら非表示にする判定の設定
11.png

攻撃してる時だけ判定、剣の向きが変だけど…みんなは上手く調整してね♪
攻撃.gif

判定の見た目を透明にする
12.png

目次に戻る

2
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
2
1