Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Door : Interactable
- {
- public AudioSource audioSource;
- public AudioClip audioClip;
- public bool shouldAutoClose = false;
- public float timeBeforeClosing;
- public float distanceBeforeClosing;
- public bool isOpen = false;
- public bool canBeInteractedWith = true;
- public bool isForcing = false;
- private Animator anim;
- private void Start()
- {
- anim = GetComponent<Animator>();
- audioSource = GetComponent<AudioSource>();
- }
- private void Update()
- {
- if (isForcing)
- {
- canBeInteractedWith = false;
- isOpen = false;
- anim.SetFloat("dot", 0);
- anim.SetBool("isOpen", isOpen);
- }
- }
- public override void OnInteract()
- {
- if(canBeInteractedWith)
- {
- isOpen = !isOpen;
- audioSource.PlayOneShot(audioClip);
- 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()
- {
- }
- public override void OnLoseFocus()
- {
- }
- 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