157 lines
4.0 KiB
C#
157 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
public class NPC : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 凝视目标
|
|
/// </summary>
|
|
public Transform AimTarget;
|
|
|
|
/// <summary>
|
|
/// 虚拟目标
|
|
/// </summary>
|
|
public Transform VirtualTarget;
|
|
|
|
/// <summary>
|
|
/// 骨骼Neck
|
|
/// </summary>
|
|
public Transform NeckTra;
|
|
|
|
/// <summary>
|
|
/// 对话内容
|
|
/// </summary>
|
|
public List<string> msg = new List<string>();
|
|
|
|
/// <summary>
|
|
/// 初始位置
|
|
/// </summary>
|
|
Vector3 oriPos;
|
|
|
|
/// <summary>
|
|
/// 自身肖像
|
|
/// </summary>
|
|
public Sprite myPortrait;
|
|
|
|
private Button mark;
|
|
private Text text;
|
|
private Button dialogBox;
|
|
private Image portrait;
|
|
|
|
int index = 1;
|
|
|
|
public Vector3 neck;
|
|
|
|
/// <summary>
|
|
/// 视线跟随
|
|
/// </summary>
|
|
public bool LineOfSightFollowing;
|
|
private void Start()
|
|
{
|
|
if (LineOfSightFollowing)
|
|
oriPos = VirtualTarget.position;
|
|
|
|
mark = transform.Find("MarkCanvas/mark").GetComponent<Button>();
|
|
|
|
text = transform.parent.Find("NPCCanvas/dialogBox/Text").GetComponent<Text>();
|
|
dialogBox = transform.parent.Find("NPCCanvas/dialogBox").GetComponent<Button>();
|
|
portrait = transform.parent.Find("NPCCanvas/dialogBox/portrait").GetComponent<Image>();
|
|
|
|
mark.onClick.AddListener(() =>
|
|
{
|
|
dialogBox.gameObject.SetActive(true);
|
|
mark.gameObject.SetActive(false);
|
|
portrait.sprite = myPortrait;
|
|
|
|
int co = UnityEngine.Random.Range(0, msg.Count);
|
|
text.text = msg[co];
|
|
|
|
dialogBox.onClick.AddListener(() =>
|
|
{
|
|
mark.gameObject.SetActive(true);
|
|
dialogBox.gameObject.SetActive(false);
|
|
dialogBox.onClick.RemoveAllListeners();
|
|
|
|
//if (index == msg.Count)
|
|
//{
|
|
// mark.gameObject.SetActive(true);
|
|
// dialogBox.gameObject.SetActive(false);
|
|
// dialogBox.onClick.RemoveAllListeners();
|
|
// index = 1;
|
|
//}
|
|
//else
|
|
//{
|
|
// text.text = msg[index];
|
|
// index++;
|
|
//}
|
|
});
|
|
});
|
|
mark.gameObject.SetActive(false);
|
|
//dialogBox.onClick.AddListener(() =>
|
|
//{
|
|
|
|
// if (index == msg.Count)
|
|
// {
|
|
// mark.gameObject.SetActive(true);
|
|
// dialogBox.gameObject.SetActive(false);
|
|
// }
|
|
// else
|
|
// {
|
|
// text.text = msg[index];
|
|
// index++;
|
|
// }
|
|
//});
|
|
}
|
|
|
|
public float Limit = 45;
|
|
private void FixedUpdate()
|
|
{
|
|
if (LineOfSightFollowing)
|
|
{
|
|
neck = NeckTra.localEulerAngles;
|
|
if (neck.y > 180)
|
|
{
|
|
if (neck.y - 360 <= -Limit)
|
|
AimTarget = null;
|
|
}
|
|
else
|
|
{
|
|
if (neck.y >= Limit)
|
|
AimTarget = null;
|
|
}
|
|
|
|
if (AimTarget != null)
|
|
{
|
|
VirtualTarget.position = Vector3.Lerp(VirtualTarget.position, AimTarget.position, 0.05f);
|
|
}
|
|
else
|
|
{
|
|
VirtualTarget.position = Vector3.Lerp(VirtualTarget.position, oriPos, 0.1f);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.transform.CompareTag("Player"))
|
|
{
|
|
AimTarget = other.transform.Find("AimTarget");
|
|
}
|
|
if (mark != null) mark.gameObject.SetActive(true);
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
if (other.transform.CompareTag("Player"))
|
|
{
|
|
AimTarget = null;
|
|
}
|
|
if (mark != null) mark.gameObject.SetActive(false);
|
|
if (dialogBox != null) dialogBox.gameObject.SetActive(false);
|
|
}
|
|
}
|