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

今回も下記参考にやっていきます。
youtu.be

今回は発砲時の銃のアニメーションやマズルフラッシュを作ります。

・プレイ中にマウス左クリックで発砲されるようにInput ActionでShootアクションを追加。

スクリプト追加。

public class InputManager : MonoBehaviour
{
 (省略)
 public bool shootInput; //EP7追加
 
 private void OnEnable() //OnEnableはオブジェクトが有効になったときに呼び出されます.
    {
        if(playerControls == null)
        {
          (省略)
            playerControls.PlayerActions.Shoot.performed += i => shootInput = true; //EP7追加
            playerControls.PlayerActions.Shoot.canceled += i => shootInput = false; //EP7追加
        }

        playerControls.Enable();
    }

public void HandleAllInputs()
    {
       (省略)
        HandleShootInput(); //EP7追加
    }

 private void HandleShootInput()
    {
        //CHECK IN FUTURE IF SEMI-AUTO OR AUTO
        if (shootInput && aimingInput)
        {
            shootInput = false;
            Debug.Log("BANG");
            //SHOOT CURRENT WEAPON
        }
    }
}

・ブローバックのアニメーションを作ります。

・武器に下記スクリプトを追加。ブローバックとマズルフラッシュの動作を実装しています。

public class WeaponAnimatorManager : MonoBehaviour //EP7追加 武器にアタッチ
{
    Animator weaponAnimator;

    [Header("weapon FX")]
    public GameObject weaponMuzzleFlashFX; //The muzzle flash FX that is instantiated when the weapon is fired.
    public GameObject weaponBulletCaseFX; //The bullet case FX that is ejected from the weapon, when the weapon is fired.

    [Header("weapon FX Transform")]
    public Transform weaponMuzzleFlashTransform; //The location the muzzle flash FX will instantiate
    public Transform weaponBulletCaseTransform; //The location the bullet case will instantiate

    private void Awake()
    {
        weaponAnimator = GetComponentInChildren<Animator>();
    }

    public void ShootWeapon(PlayerCamera playerCamera)
    {
        //ANIMATE THE WEAPON
        weaponAnimator.Play("Shoot");

        //INSTANTIATE MUZZLE FLASH FX
        GameObject muzzleFlash = Instantiate(weaponMuzzleFlashFX.gameObject, weaponMuzzleFlashTransform);
        muzzleFlash.transform.parent = null;
     
        //INSTANTIATE EMPTY BULLET CASE
      //  GameObject bulletCase = Instantiate(weaponBulletCaseFX, weaponBulletCaseTransform);
      //  bulletCase.transform.parent = null;

        //SHOOT SOMETHING
        RaycastHit hit;
        if(Physics.Raycast(playerCamera.cameraObject.transform.position, playerCamera.cameraObject.transform.forward,out hit))
        {
            Debug.Log(hit.transform.gameObject.name);
        }
    }
}

RayCastについては下記参照。
unity.moon-bear.com

・マズルフラッシュと弾薬が排出される位置を下記のように作り、上記スクリプトのpublic transformに設定する。

・下記を参考にマズルフラッシュFXを作成。
unity.moon-bear.com

・ WeaponAnimatorManagerクラスをPlayerEquipmentManagerクラス内で読み込む。

public class PlayerEquipmentManager : MonoBehaviour //EP4追加.playerにアタッチ
{
    (省略)
    public WeaponAnimatorManager weaponAnimator; //EP7追加

 void LoadCurrentWepon()
    {
        //LOAD THE WEPON ONTO PLAYERS HANDS
        weaponLoaderSlot.LoadWeaponModel(weapon);

        //CHANGE OUR PLAYERS MOVEMENT/IDLE ANIMSTIONS TO THE WEPONS MOVEMENT/IDLE ANIMATIONS
        animatorManager.animator.runtimeAnimatorController = weapon.weponAnimatior;

        weaponAnimator = weaponLoaderSlot.currentWeaponModel.GetComponentInChildren<WeaponAnimatorManager>(); //EP7追加
 (省略)
    }
}

・下記スクリプト追加。

public class PlayerManager : MonoBehaviour
{
 (省略)
PlayerEquipmentManager playerEquipmentManager; //EP7追加

 private void Awake()
    {
      (省略)
        playerEquipmentManager = GetComponent<PlayerEquipmentManager>(); //EP7追加
    }

//EP7追加 inputManagerで実行
    public void UseCurrentWeapon() 
    {
        if (isPreformingAction)
            return;

        //In the future wil and the option to use knives also
        playerEquipmentManager.weaponAnimator.ShootWeapon(playerCamera);
    }
}

・InputManagerクラスに下記追加。

 private void HandleShootInput()
    {
        //CHECK IN FUTURE IF SEMI-AUTO OR AUTO
        if (shootInput && aimingInput)
        {
            shootInput = false;
          //  Debug.Log("BANG");
            //SHOOT CURRENT WEAPON
            playerManager.UseCurrentWeapon(); //今回追加
        }
    }

・マズルフラッシュFXに下記スクリプトをアタッチし、設定した時間でFXオブジェクトを消失させます。

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

public class DestroyAfterTime : MonoBehaviour //武器FXにアタッチ
{
    public float time = 1f;

    private void Awake()
    {
        Destroy(gameObject, time);
    }
}

これで発砲時のマズルフラッシュとブローバックが出来ました。
youtu.be