Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerDialog : MonoBehaviour
- {
- [SerializeField] private Dialogue _dialogue;
- [SerializeField] private DialoguePanelController _dialoguePanelController;
- [SerializeField] private float _range;
- [SerializeField] private float _dialogOutDistance;
- private PlayerInventory _inventory;
- private void Start()
- {
- _inventory = GetComponent<PlayerInventory>();
- }
- private void Update()
- {
- Look();
- }
- private void Look()
- {
- var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, _range))
- {
- if (hit.collider.TryGetComponent(out DialogueNPC dialogueNPC) && Input.GetKeyDown(KeyCode.E))
- {
- if (_dialogue.GetDialogueNpc() == null)
- {
- _dialogue.SetNPC(dialogueNPC);
- _dialoguePanelController.EnablePanel();
- _dialogue.NextPhrase(_inventory);
- }
- else if (dialogueNPC != _dialogue.GetDialogueNpc())
- {
- _dialogue.SetNPC(dialogueNPC);
- _dialogue.ResetPhrases();
- _dialogue.NextPhrase(_inventory);
- }
- else if (_dialogue.GetDialogueNpc() != null && _dialogue.CurrentIndex < _dialogue.GetDialogueNpc().GetPhrasesCount() - 1)
- {
- _dialogue.NextPhrase(_inventory);
- }
- else
- {
- ResetDialogue();
- }
- }
- if (_dialogue.GetDialogueNpc() != null &&
- Vector3.Distance(transform.position, _dialogue.GetDialogueNpc().transform.position) > _dialogOutDistance)
- {
- ResetDialogue();
- }
- }
- }
- private void ResetDialogue()
- {
- _dialogue.ResetNPC();
- _dialogue.ResetPhrases();
- _dialoguePanelController.DisablePanel();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment