【サバイバルホラーゲームを作りたい。その12】

今回もこちらの動画をやっていきます。
youtu.be

今回は敵ゾンビのHPと倒れる動作を実装してます。

スクリプトは下記です。

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

public class ZombieDeath : MonoBehaviour //EP12追加 ゾンビにアタッチ
{
    public int EnemyHealth = 20;
    Animator animator;
    public int StatusCheck;

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

    void DamageZombie(int DamageAmount)
    {
        EnemyHealth -= DamageAmount;
        Debug.Log("hit");
    }

    // Update is called once per frame
    void Update()
    {
        if(EnemyHealth<=0 && StatusCheck == 0)
        {
            StatusCheck = 2;
            animator.Play("Z_FallingBack");
        }
    }
}
public class FirePistol : MonoBehaviour //EP11追加 Gunにアタッチ
{
    public InputScript inputScript;
    Animator animator;
    public AudioSource pistolSound;

    public float TargetDistance;
    public int DamageAmount = 5;

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

    // Update is called once per frame
    void Update()
    {
        //左マウスが押された場合
        if (inputScript.getFireInput())
        {
            StartCoroutine(FiringPistol());
        }
    }

    IEnumerator FiringPistol()
    {
        inputScript.setFireInput(false);

       

        RaycastHit shot;
        // public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo)
        // origin:ワールド座標でのレイの開始地点, direction:レイの方向, hitInfo:衝突した相手オブジェクトの情報
        //EP12追加
        if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.forward), out shot))
            {
            //レイの開始点からレイ衝突した点までの距離
            TargetDistance = shot.distance;

            //ゲームオブジェクトにアタッチされているすべての MonoBehaviour にある methodName と名付けたメソッドを呼び出します
            shot.transform.SendMessage("DamageZombie", DamageAmount, SendMessageOptions.DontRequireReceiver);
        }

        animator.Play("PistolShot");
        animator.Play("MuzzleFlashAnim");
        pistolSound.Play();

        yield return null;
    }
}

・ゾンビアニメーションの設定です。

プレイ動画です。
youtu.be


今回は以上です。