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

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

今回はカギを所持している場合に、ドアを開くことを可能にします。

・カギの有無を保存するスクリプトを追加します。

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

public class GlobalInventory : MonoBehaviour //EP32追加
{
    [SerializeField]
    bool Doorkey001 = false;

    public bool getDoorKey001()
    {
        return Doorkey001;
    }

    public void setDoorKey001(bool b)
    {
        this.Doorkey001 = b;
    }
}

・ドアオープンを実装します。

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

public class LockedDoor : MonoBehaviour //EP31追加
{
    public GameObject actionKey;
    public GameObject actionText;
    Text actiontext;
    public InputScript inputScript;
    public AudioSource CreakSound;

    BoxCollider boxCollider;
    public Animator animator;
    public GameObject ExtraCross;

    public GlobalInventory globalInventory;

    private void Start()
    {
        boxCollider = GetComponent<BoxCollider>();
        actiontext = actionText.GetComponent<Text>();
    }

    private void OnTriggerEnter(Collider other)
    {
        //コライダーにプレイヤーが当たった場合
        if (other.tag == "Player")
        {
            // Debug.Log("hit");

            //キー押下~のメッセージを表示
            actionKey.SetActive(true);
            actionText.SetActive(true);
            actiontext.text = "Open The Door";

            ExtraCross.SetActive(true); //EP7追加
        }
    }

    private void OnTriggerStay(Collider other)
    {
        if (other.tag == "Player" && inputScript.getInputActions())
        {
            //カギなしの場合
            if (!globalInventory.getDoorKey001())
            {
                actiontext.text = "The Door is Locked";
            }

            //カギありの場合
            else if (globalInventory.getDoorKey001())
            {
                actionKey.SetActive(false);
                actionText.SetActive(false);

                boxCollider.enabled = false;
                animator.Play("FirstDoorOpenAnim");

                //ドアが開く音
                CreakSound.Play();
                ExtraCross.SetActive(false);
                boxCollider.gameObject.SetActive(false);
            }
        }
    }

    ////コライダーからプレイヤーが離れた場合
    private void OnTriggerExit(Collider other)
    {
        actionKey.SetActive(false);
        actionText.SetActive(false);
        ExtraCross.SetActive(false); 
    }
}

今回は以上です。