【バイオハザード風のゲームを作ってみよう。その4】
今回もやっていきましょう。
今回はハンドガンをスポーンして構えるようにしていきます。
下記便利な機能を使用しているので勉強になりました。
・ScriptableObject /CreateAssetMenu
・Animator Override Controller
下記を参考にしています。
youtu.be
前回まで、はじめから銃を構えているアニメーションを使用していた為、Animatorにセットしているidle/前進/後進/Runのアニメーションを手ぶらのものに変更します。
つぎに、ScriptableObjectクラスを継承したItemクラスを作ります。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Item : ScriptableObject //EP4追加。ScriptableObjectは基本的にゲーム中に変化しない値を登録しておくのに使うデータベースのようなもの { [Header("Item Information")] public string itemName; public GameObject itemModel; }
ScriptableObjectクラスについての詳細は下記参照。
kurokumasoft.com
そしてItemクラスを継承したWeponItemクラスを作成します。
using System.Collections; using System.Collections.Generic; using UnityEngine; [CreateAssetMenu(menuName ="Items/Weapon Item")] public class WeaponItem : Item //EP4追加 { [Header("Weapon Animation")] public AnimatorOverrideController weponAnimatior; //今ある Animator Controllerの拡張を可能にするアセット }
ScriptableObject /CreateAssetMenuについて、下記を参考にしました。
www.urablog.xyz
つぎに、WeponItemクラスに設定したweponオブジェクトを受け取り、スポーンさせるスクリプト(WeaponLoaderSlot.cs)を作成し、プレイヤーの右手にアタッチします。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class WeaponLoaderSlot : MonoBehaviour //EP4追加.playerのRight Handにアタッチ { public GameObject currentWeaponModel; private void UnLoadAndDestroyWeapon() { if (currentWeaponModel != null) { Destroy(currentWeaponModel); } } //渡されたweponをスポーンする。 public void LoadWeaponModel(WeaponItem weapon) { UnLoadAndDestroyWeapon(); if (weapon == null) { return; } GameObject weaponModel = Instantiate(weapon.itemModel, transform); weaponModel.transform.localPosition = Vector3.zero; weaponModel.transform.localRotation = Quaternion.identity; //Quaternion.identity:このクォータニオンは「回転していない」を表す。 weaponModel.transform.localScale = Vector3.one; currentWeaponModel = weaponModel; } }
次に、PlayerEquipmentManagerスクリプトを作成し、プレイヤーにアタッチします。
このスクリプトは、武器のスポーンや後述するPlayer_Controllerからオーバーライドしたアニメーターコントローラー(Pistol)への切り替えを行っています。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerEquipmentManager : MonoBehaviour //EP4追加.playerにアタッチ { AnimatorManager animatorManager; WeaponLoaderSlot weaponLoaderSlot; [Header("Current Equipment")] public WeaponItem weapon; //public SubweponItem ;//knife,stun grenads ect private void Awake() { animatorManager = GetComponent<AnimatorManager>(); LoadWeaponLoaderSlots(); } private void Start() { LoadCurrentWepon(); } private void LoadWeaponLoaderSlots() { //Bsck Slot //Hip Slot //子オブジェクトからWeaponLoaderSlotコンポーネントを取得。 weaponLoaderSlot = GetComponentInChildren<WeaponLoaderSlot>(); } 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; } }
Pistolアニメーターコントローラー
idleの部分にハンドガンを構えているアニメーションを設定することで、Pistolコントローラーが呼ばれた場合、idleのアニメーションが設定したアニメーションに置き換わります。
次に、ハンドガンをシーンに置き、プレイヤーの右手の位置になるように調整します。
そして、Projectビュー右クリック時からWeponItem型ののScriptableObject(Pistol)を作成します。
これで、はじめはハンドガンを構えているが、歩くと普通に歩くようになりました。
今後、歩行時も武器を持っているアニメーションに設定していくことになると思います。
youtu.be