【バイオハザード風のゲームを作ってみよう。その18】

今回も下記動画をやっていきます。
youtu.be

今回はゾンビに数回掴まれたプレイヤーの死亡動作を実装していきます。

・死亡アニメーションを準備します。私はmixamo”Dying Backwards”を使用しました。

・ゾンビの攻撃ダメージを設定します。

public class ZombieCombatManager : MonoBehaviour //EP17追加 ゾンビにアタッチ
{
    [Header("Attack Damage")] //EP18追加
    public int grappleBiteDamage = 33; //ゾンビの噛み攻撃のダメージ
}

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

public class PlayerStatManager : MonoBehaviour //EP18追加 プレイヤーにアタッチ
{
    PlayerManager player;

    [Header("Health")]
    public int playerHealth = 100;

    [Header("Pending Damage")]
    public int pendingDamage = 0;

    private void Awake()
    {
        player = GetComponent<PlayerManager>();
    }

    private void KillPlayer()
    {
        if (!player.isPreformingAction)
        {
            player.animatorManager.PlayAnimation("Player_Dead", true);
        }

        player.isDead = true;
    }

    public void TakeDamageFromGrapple()
    {
        playerHealth = playerHealth - pendingDamage;

        if (playerHealth <= 0)
        {
            KillPlayer();
        }
    }
}
public class PlayerManager : MonoBehaviour
{
 public PlayerStatManager playerStatManager; //EP18追加

 [Header("Status")]
    public bool isDead; //EP18追加 プレイヤーが死んだ場合、true

 private void Awake()
    {
        playerStatManager = GetComponent<PlayerStatManager>(); //EP18追加
    }
}
public class ZombieGrappleCollider : MonoBehaviour //EP17追加 ゾンビ右手/左手直下のRight Hnad grapple Collider/Left Hnad grapple Colliderにアタッチ
{
private void OnTriggerEnter(Collider other)
    {
        //If we grab a collider on the "Player" layer,we proceed
        if (other.gameObject.layer == LayerMask.NameToLayer("Player"))
        {
            PlayerManager player = other.GetComponent<PlayerManager>();

            if (player != null)
            {
                //if the player is not already performing an action, we proceed
                if (!player.isPreformingAction)
                {
                   (省略)
                    //Play the player's grapple animation
                    player.animatorManager.ClearHandIKWeights();
                    player.animatorManager.PlayAnimation("Player Grapple", true);
                    player.playerStatManager.pendingDamage = zombie.zombieCombatManager.grappleBiteDamage; //EP18追加

                }
            }
        }
    }
}
public class AttackState : State //EP10追加 ゾンビ直下のstatesオブジェクトにアタッチ
{

  public override State Tick(ZombieManager zombieManager)
    {
        zombieManager.animator.SetFloat("Vertical", 0f, 0.2f, Time.deltaTime);

        //If our target is dead, return here and nothing.
        //プレイヤーが死んでいる場合、以降の処理を行わない
        if (zombieManager.currentTarget.isDead) //EP18追加
        {
            return this;
        }
  (省略)
    }
}
public class IdleState : State //EP8追加 ゾンビ直下のstatesオブジェクトにアタッチ
{
 private void FindATargetViaLineOfSight(ZombieManager zombieManager)
    {
 (省略)
          //For every collider that we find, that is on the same layer pf the player, we try and serch it for PlayerManager script
        for(int i = 0; i < colliders.Length; i++)
        {
            (省略)
            if (player != null)
            {
             (省略)

                if(viewableAngle>minimumDetectionRadiusAngle && viewableAngle < maximumDetectionRadiusAngle) 
                {
                 (省略)
                    if (Physics.Linecast(playerStartPoint,zombieStartPoint,out hit, ignoreForLineOfSightDetection)) //EP10変更
                    {
                      //  Debug.Log("There is something iin the way");
                        //Cannot find the target, there is an object in the way
                                            }
                    else
                    {
                        //  Debug.Log("We have a target, switching states");
                        if (!player.isDead) //EP18追加
                        {
                            zombieManager.currentTarget = player;
                        }
                        
                    }
                }
            }
        }
    }
}

プレイ動画です
youtu.be

今回は以上です。