_xiaofang/xiaofang/Assets/Script/npc/RecuseNpc.cs

98 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}