Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- namespace DefaultNamespace
- {
- public class DoorActivator : MonoBehaviour
- {
- private const string Player = "Player";
- private static readonly int Close = Animator.StringToHash("Close");
- private static readonly int Open = Animator.StringToHash("Open");
- public GameObject Canvas;
- public Animator Door;
- private Animator _anim;
- private bool _canOpenDoor;
- private bool _doorIsOpened;
- [SerializeField] private KeyCode _activationKey = KeyCode.E;
- private void Start()
- {
- _anim = Door.GetComponent<Animator>();
- }
- private void Update()
- {
- if (Input.GetKeyDown(_activationKey) && _canOpenDoor && _doorIsOpened)
- {
- _doorIsOpened = !_doorIsOpened;
- if (_doorIsOpened)
- _anim.SetTrigger(Close);
- else
- _anim.SetTrigger(Open);
- _doorIsOpened = false;
- }
- }
- private void OnTriggerStay(Collider other)
- {
- if (other.CompareTag(Player))
- {
- Canvas.SetActive(true);
- _canOpenDoor = true;
- }
- }
- private void OnTriggerExit(Collider other)
- {
- if (other.CompareTag(Player))
- {
- Canvas.SetActive(false);
- _canOpenDoor = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement