2
2

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.

[Unity3D] ダメージの実装

Posted at

当たり判定をつける

1, Playerの当たり判定をつけたい部位にMeshRenderer、MeshColliderかBoxColliderを付ける
メッシュの形、大きさを調整する
E599FE31-DB9B-4BF3-8245-F5929249080A.jpeg
22DFA714-16A0-44E1-8335-7B79B8D46EA1.jpeg

2, 敵にCapsuleColliderを付け、isTriggerをチェック、Colliderの大きさを調整する
2E995B94-E9B2-48FA-8236-5FA0625D31AA.jpeg
2719328B-7D0C-45DB-A772-D43370499E05_4_5005_c.jpeg

3, EnemyMnagerに下記スクリプトを追加し、動作確認する

EnemyManager
private void OnTriggerEnter(Collider other) // 当たり判定
    {
        Debug.Log("接触(敵)");
    }

4, 敵の部位にBoxColliderを付ける
367F8A1A-56A6-4243-9607-9A4760C157F8_4_5005_c.jpeg

5, プレーヤーにCapsuleColliderを付ける
DD2FDEFC-61F3-4170-A235-2FD465DE89DA.jpeg

6, PlayerManagerに下記スクリプトを追加し、動作確認する

PlayerManager
private void OnTriggerEnter(Collider other) // 当たり判定
    {
        Debug.Log("接触(プレーヤー)");
    }

ダメージを実装

1, Damager.csを作成し、各部位に設定する
11214321-7CAD-4E4C-AF37-E9CF3A5BF4E3.jpeg

2, Damager.cs、Playermanager,Enemymanagerに下記のスクリプトを記述

Damager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Damager : MonoBehaviour
{
    public int damage;
}
PlayerManager
private void OnTriggerEnter(Collider other)
    {
        Damager damager = other.GetComponent<Damager>();
        if (damager != null)
        {
            //ダメージを与えるものにぶつかったら
            Debug.Log("プレーヤーはダメージをうける");
        }
    }
EnemyManager
private void OnTriggerEnter(Collider other)
    {
        Damager damager = other.GetComponent<Damager>();
        if (damager != null)
        {
            //ダメージを与えるものにぶつかったら
            Debug.Log("敵はダメージをうける");
        }
    }

ダメージアニメーションの実装

1, アニメーションを配置し、AnyStateからMakeTransitionをつける
055FB971-A60B-4A99-BC31-B00C2212A99F_4_5005_c.jpeg

2, PrametersにHurtを作り、各項目を設定する
1913159A-321D-437B-8F54-6501D34FB6A5.jpeg

3, 下記コメント部分のコードを追加

PlayerManager
private void OnTriggerEnter(Collider other)
    {
        Damager damager = other.GetComponent<Damager>();
        if (damager != null)
        {
            //ダメージを与えるものにぶつかったら
            animator.SetTrigger("Hurt");
        }
    }

4, 敵も同様にアニメーションを実装する
E5E0940E-4C6D-4D95-B3A8-78B195B3D55D.jpeg

EnemyManager
private void OnTriggerEnter(Collider other)
    {
        Damager damager = other.GetComponent<Damager>();
        if (damager != null)
        {
            //ダメージを与えるものにぶつかったら
            animator.SetTrigger("Hurt");
        }
    }

攻撃の判定

1, Playerのanimationを開く
E5D6A0BA-1B60-4F3D-8CCB-944FCD439318.jpeg

2, playerManagerに下記コメント部分のスクリプトを書く

PlayerManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerManager : MonoBehaviour
{
    float x;
    float z;
    public float moveSpeed;
    public Collider punchCollider; //設定する
    public Collider kickCollider;

    Rigidbody rb;
    Animator animator;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        animator = GetComponent<Animator>();
        HideColliderPunch();   //最初は隠しておく
        HideColliderKick();
    }

    void Update()
    {
        x = Input.GetAxisRaw("Horizontal");
        z = Input.GetAxisRaw("Vertical");

        //攻撃
        if (Input.GetKeyDown(KeyCode.V))
        {
            animator.SetTrigger("Attack");
        }
        else if (Input.GetKeyDown(KeyCode.C))
        {
            animator.SetTrigger("Kick");
        }
        else if (Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetTrigger("Jump");
        }
        else if (Input.GetKeyDown(KeyCode.M))
        {
            animator.SetTrigger("Brooklyn");
        }
    }

    void FixedUpdate()
    {
        Vector3 direction = transform.position + new Vector3(x, 0, z) * moveSpeed;
        transform.LookAt(direction);

        rb.velocity = new Vector3(x, 0, z) * moveSpeed;
        animator.SetFloat("Speed", rb.velocity.magnitude);
    }

    //攻撃の判定
    public void HideColliderPunch()
    {
        punchCollider.enabled = false; //無効にする
    }
    public void ShowColliderPunch()
    {
        punchCollider.enabled = true;  //有効にする
    }
    public void HideColliderKick()
    {
        kickCollider.enabled = false; //無効にする
    }
    public void ShowColliderKick()
    {
        kickCollider.enabled = true;  //有効にする
    }

    private void OnTriggerEnter(Collider other)
    {
        Damager damager = other.GetComponent<Damager>();
        if (damager != null)
        {
            animator.SetTrigger("Hurt");
        }
    }
}

3, 各攻撃にAdd eventで始まりと終わりを部分をつけ、Function部分でShowColliderPunch(), HideColliderPunch()を設定する
F51E7D93-968E-4609-BF67-A65B6AEB788D.jpeg

307FFF67-435C-4FC1-AFD8-42589DBA0005.jpeg

4, 各Colliderがついているものを設定する
C19624AA-680B-4458-9463-E4DEE7086D13.jpeg

5, 敵も同様に設定する

プレイヤーが攻撃を受けても移動出来てしまうバグ修正

1, PlayerのGetHitを選択し、PlayerHurtBehaviourを作成する
85CD48CF-CC57-4088-ADBF-D1A5FF43AA8A_4_5005_c.jpeg

2, 下記コードを記述

PlayerHurtBehaviour
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAttackBehaviour : 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.GetComponent<PlayerManager>().moveSpeed = 0;
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {

    }

    // 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.GetComponent<PlayerManager>().moveSpeed = 3;
    }

    // OnStateMove is called right after Animator.OnAnimatorMove()
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that processes and affects root motion
    //}

    // OnStateIK is called right after Animator.OnAnimatorIK()
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that sets up animation IK (inverse kinematics)
    //}
}

攻撃を受けた時に何度もアニメーションが実行されてしまうバグ修正

1, EnemyHurtBehaviourとplayerHurtBehaviourに下記コメント部分のコードを追加する

EnemyHurtBehaviour
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyHurtBehaviour : 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.ResetTrigger("Hurt"); //攻撃を受けたらリセットする
        animator.GetComponent<NavMeshAgent>().speed = 0;
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{

    //}

    // 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.ResetTrigger("Hurt"); //抜けるときもリセットする
    }
2
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?