Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
using UnityEngine;
using System.Collections;
public class NpcTalk : MonoBehaviour {
  public GameObject npc; //assign this in the inspector
  public GameObject npcDialogui;
  public Animator anim;
  public AudioSource audioSource;
  float interactDist = 5.5f;
  void Update() {
    TalkToNpc();
  }
  Coroutine talkToNPC = null;
  void TalkToNpc() {
    float dist = Vector3.Distance(npc.transform.position, transform.position);
    if (dist < interactDist) {
      if (Input.GetKeyDown(KeyCode.E) && talkToNPC == null) {
        talkToNPC = StartCoroutine(DoTalk());
      }
    }
    if (dist > interactDist) {
      npcDialogui.SetActive(false);
      if (talkToNPC != null) {
        StopCoroutine(talkToNPC);
        talkToNPC = null;
      }
    }
  }
  IEnumerator DoTalk() {
    anim.SetTrigger("Talk");
    Debug.Log("i wonder what all these buttons do maby u should jump on them");
    AudioPlayer.Instance.PlayAudioClip(0);
    npcDialogui.SetActive(true);
    talkToNPC = null;
    GetComponent<PlayerMovement>().enabled = false;
    if (Input.GetKeyDown(KeyCode.E) && talkToNPC == null)
      yield return new WaitForSeconds(4f);
    GetComponent<PlayerMovement>().enabled = true;
    anim.<Walk>().enabled = false;
    if (Input.GetKeyDown(KeyCode.E) && talkToNPC == null)
      yield return new WaitForSeconds(4f);
    anim.<Walk>().enabled = true;
  }
}