Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class Door : Interactable
- {
- [Header("Key")]
- [SerializeField] string sentence;
- [SerializeField] bool isUsingKey = false;
- [SerializeField] GameObject key = default;
- [Header("Audio")]
- [SerializeField] AudioSource audioSource;
- [SerializeField] AudioClip[] openSound;
- [SerializeField] AudioClip closeSound;
- [SerializeField] AudioClip lockSound;
- [Header("Auto Close")]
- [SerializeField] bool shouldAutoClose = false;
- [SerializeField] float timeBeforeClosing;
- [SerializeField] float distanceBeforeClosing;
- public bool shouldCloseAfterOpen = false;
- public bool isOpen = false;
- public bool canBeInteractedWith = true;
- public bool showHand = true;
- public bool isForcing = false;
- private bool forceAudioOnce = true;
- private Animator anim;
- private TextMeshProUGUI textDisplay;
- private void Start()
- {
- anim = GetComponent<Animator>();
- audioSource = GetComponent<AudioSource>();
- textDisplay = GameObject.FindGameObjectWithTag("MainText").GetComponent<TextMeshProUGUI>();
- }
- private void Update()
- {
- if (isForcing)
- {
- anim.SetBool("isForce", true);
- isUsingKey = true;
- showHand = true;
- isOpen = false;
- anim.SetFloat("dot", 0);
- if (forceAudioOnce == true)
- {
- audioSource.PlayOneShot(closeSound);
- forceAudioOnce = false;
- }
- }
- else
- {
- anim.SetBool("isForce", false);
- }
- if (shouldCloseAfterOpen && isOpen == true)
- {
- canBeInteractedWith = false;
- showHand = false;
- }
- }
- public override void OnInteract()
- {
- if (isUsingKey && showHand)
- {
- textDisplay.text = sentence;
- if (key.activeInHierarchy == false)
- {
- audioSource.PlayOneShot(lockSound);
- canBeInteractedWith = false;
- }
- else
- {
- canBeInteractedWith = true;
- key.SetActive(false);
- }
- }
- if (canBeInteractedWith)
- {
- isUsingKey = false;
- PlayerController.instance.interactOB.SetActive(false);
- textDisplay.text = "";
- if (isOpen)
- {
- audioSource.PlayOneShot(closeSound);
- }
- if (isOpen == false)
- {
- audioSource.PlayOneShot(openSound[Random.Range(0, openSound.Length)]);
- }
- isOpen = !isOpen;
- Vector3 playerTransformDirection = PlayerController.instance.transform.position - transform.position;
- Vector3 doorTransformDirection = transform.TransformDirection(Vector3.forward);
- float dot = Vector3.Dot(playerTransformDirection, doorTransformDirection);
- Debug.Log(dot);
- anim.SetFloat("dot", dot);
- anim.SetBool("isOpen", isOpen);
- if (shouldAutoClose)
- {
- StartCoroutine(AutoClose());
- }
- }
- }
- public override void OnFocus()
- {
- if (showHand)
- {
- PlayerController.instance.interactOB.SetActive(true);
- }
- }
- public override void OnLoseFocus()
- {
- textDisplay.text = "";
- }
- private void Animator_LockInteraction()
- {
- canBeInteractedWith = false;
- }
- private void Animator_UnlockInteraction()
- {
- canBeInteractedWith = true;
- }
- private IEnumerator AutoClose()
- {
- while (isOpen)
- {
- yield return new WaitForSeconds(timeBeforeClosing);
- if (Vector3.Distance(transform.position, PlayerController.instance.transform.position) > distanceBeforeClosing)
- {
- isOpen = false;
- anim.SetFloat("dot", 0);
- anim.SetBool("isOpen", isOpen);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment