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

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

今回はカギ付きドアを設置します。

・Main Sceneで設置しているドアをコピー&ペーストします。

・カギ取得の動作を実装します。

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

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

    BoxCollider boxCollider;
    public GameObject ExtraCross;

    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 = "Pick Up Key";

            ExtraCross.SetActive(true);
        }
    }

    private void OnTriggerStay(Collider other)
    {
        if (inputScript.getInputActions())
        {
            actionKey.SetActive(false);
            actionText.SetActive(false);
            ExtraCross.SetActive(false);
            this.transform.gameObject.SetActive(false);
        }
    }

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

今回は以上です。