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

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

今回はゾンビの攻撃アクションを実装していきます。

・下記スクリプトを追加します。

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

public class ZombieAi : MonoBehaviour //EP13追加 ゾンビにアタッチ
{
    public GameObject thePlayer;
    Animator EnemyAnimator;
    public float enemySpeed = 0.01f;
    public bool attackTrigger = false;
    public bool isAttacking = false;

    bool walkOn = true;

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

    // Update is called once per frame
    void Update()
    {
        transform.LookAt(thePlayer.transform);

        if (!attackTrigger && walkOn)
        {
            enemySpeed = 0.01f;
            //歩行アニメーション
            EnemyAnimator.Play("Z_Walk");
           
            //ターゲット方向に移動
            transform.position = Vector3.MoveTowards(transform.position, thePlayer.transform.position, enemySpeed);
            StartCoroutine(walk());
        }

        if (attackTrigger && !isAttacking)
        {
            enemySpeed = 0;
            //攻撃アニメーション
            EnemyAnimator.Play("Z_Attack");
            
            StartCoroutine(InflactDamage());
        }
    }

    void OnTriggerEnter()
    {
        attackTrigger = true;
    }

    void OnTriggerExit()
    {
        attackTrigger = false;
    }

    IEnumerator InflactDamage()
    {
        isAttacking = true;
        yield return new WaitForSeconds(1.1f);
        GlobalHealth.currentHealth -= 5;
        yield return new WaitForSeconds(0.2f);
        isAttacking = false;
    }

    IEnumerator walk()
    {
        walkOn = false;
        yield return null;
        walkOn = true;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GlobalHealth : MonoBehaviour //EP13追加 プレイヤーにアタッチ
{
    public static int currentHealth = 20;
    public int internalHealth;

    // Update is called once per frame
    void Update()
    {
        internalHealth = currentHealth;
    }
}


・ゾンビアニメータコントローラの設定です。

プレイ動画です。
youtu.be

今回は以上です。