98 lines
2.2 KiB
C#
98 lines
2.2 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public enum Npcstate
|
||
{
|
||
idle,
|
||
dead,
|
||
run
|
||
}
|
||
|
||
public class RecuseNpc : MonoBehaviour
|
||
{
|
||
private Button recusebtn;
|
||
|
||
private bool statebool = false;
|
||
|
||
private Animator anim;
|
||
|
||
public Npcstate nstate = Npcstate.dead;
|
||
|
||
private bool movebool = false;
|
||
public Transform target;
|
||
private void Awake()
|
||
{
|
||
recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
|
||
anim = this.GetComponent<Animator>();
|
||
|
||
}
|
||
|
||
private void OnTriggerEnter(Collider other)//通过触发器得到对应的救援组人员标签,是否显现出UI
|
||
{
|
||
if(other.tag == "Player")
|
||
recusebtn.gameObject.SetActive(true);
|
||
if (statebool) return;
|
||
other.GetComponent<CharacterInturn>().cha = this.gameObject ;
|
||
}
|
||
|
||
private void OnTriggerExit(Collider other)
|
||
{
|
||
recusebtn.gameObject.SetActive(false);
|
||
}
|
||
|
||
public void Setnpcstate()//点击救援按钮执行完动作后对按钮进行隐藏
|
||
{
|
||
|
||
movebool = true;
|
||
nstate = Npcstate.run;
|
||
recusebtn.gameObject.SetActive(false);
|
||
Debug.Log("Setnpcstate调用");
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (Vector3.Distance(transform.position,target.position) < 1.3f && movebool && target.transform.position != null)//判断人物被救援后移动到目标位置
|
||
{
|
||
Debug.Log("到达目标点");
|
||
nstate = Npcstate.idle;
|
||
movebool = false;
|
||
}
|
||
|
||
|
||
switch (nstate)//通过枚举状态实现人物是否被救援,以及动作的改变
|
||
{
|
||
case Npcstate.idle:
|
||
SetAni(2);
|
||
break;
|
||
case Npcstate.run:
|
||
movebool = true;
|
||
Run();
|
||
break;
|
||
case Npcstate.dead:
|
||
SetAni(0);
|
||
break;
|
||
}
|
||
}
|
||
|
||
//跑步逻辑
|
||
public void Run()
|
||
{
|
||
if (movebool)
|
||
{
|
||
SetAni(1);
|
||
transform.LookAt(target);
|
||
transform.position = Vector3.Lerp(transform.position, target.position, 0.3f * Time.deltaTime);
|
||
}
|
||
}
|
||
|
||
|
||
public void SetAni(int x)
|
||
{
|
||
anim.SetInteger("state", x);
|
||
}
|
||
|
||
|
||
}
|