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

98 lines
2.2 KiB
C#
Raw Normal View History

2024-10-21 18:09:07 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2024-10-22 11:57:50 +08:00
2024-10-22 17:37:42 +08:00
public enum Npcstate
{
idle,
dead,
2024-11-29 15:13:32 +08:00
run
2024-10-22 11:57:50 +08:00
}
2024-10-21 18:09:07 +08:00
public class RecuseNpc : MonoBehaviour
{
private Button recusebtn;
2024-10-22 11:57:50 +08:00
2024-10-22 17:37:42 +08:00
private bool statebool = false;
private Animator anim;
2024-10-22 11:57:50 +08:00
2024-11-29 15:13:32 +08:00
public Npcstate nstate = Npcstate.dead;
2024-10-22 11:57:50 +08:00
2024-10-22 17:37:42 +08:00
private bool movebool = false;
public Transform target;
2024-10-22 11:57:50 +08:00
private void Awake()
2024-10-21 18:09:07 +08:00
{
recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
2024-10-22 17:37:42 +08:00
anim = this.GetComponent<Animator>();
2024-10-21 18:09:07 +08:00
}
2024-10-22 17:37:42 +08:00
private void OnTriggerEnter(Collider other)//ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>Ӧ<EFBFBD>ľ<EFBFBD>Ԯ<EFBFBD><D4AE><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1>ǩ<EFBFBD><C7A9><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ֳ<EFBFBD>UI
2024-10-21 18:09:07 +08:00
{
2024-10-24 18:01:55 +08:00
if(other.tag == "Player")
2024-10-21 18:09:07 +08:00
recusebtn.gameObject.SetActive(true);
2024-10-22 11:57:50 +08:00
if (statebool) return;
2024-10-24 12:46:20 +08:00
other.GetComponent<CharacterInturn>().cha = this.gameObject ;
2024-10-22 11:57:50 +08:00
}
2024-10-21 18:09:07 +08:00
2024-10-23 11:31:35 +08:00
private void OnTriggerExit(Collider other)
{
recusebtn.gameObject.SetActive(false);
}
2024-10-22 17:37:42 +08:00
public void Setnpcstate()//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԯ<EFBFBD><D4AE>ťִ<C5A5><D6B4><EFBFBD><EFBFBD><EAB6AF><EFBFBD><EFBFBD><EFBFBD>԰<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
2024-11-29 15:13:32 +08:00
2024-10-22 17:37:42 +08:00
movebool = true;
2024-11-29 15:13:32 +08:00
nstate = Npcstate.run;
2024-10-22 17:37:42 +08:00
recusebtn.gameObject.SetActive(false);
Debug.Log("Setnpcstate<74><65><EFBFBD><EFBFBD>");
}
private void Update()
2024-10-22 11:57:50 +08:00
{
if (Vector3.Distance(transform.position,target.position) < 1.3f && movebool && target.transform.position != null)//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFB1BB>Ԯ<EFBFBD><D4AE><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>
2024-10-22 17:37:42 +08:00
{
Debug.Log("<22><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>");
nstate = Npcstate.idle;
movebool = false;
}
switch (nstate)//ͨ<><CDA8>ö<EFBFBD><C3B6>״̬ʵ<CCAC><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ񱻾<C7B7>Ԯ<EFBFBD><D4AE><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸı<C4B8>
{
case Npcstate.idle:
2024-11-29 15:13:32 +08:00
SetAni(2);
2024-10-22 17:37:42 +08:00
break;
2024-11-29 15:13:32 +08:00
case Npcstate.run:
movebool = true;
Run();
2024-10-22 17:37:42 +08:00
break;
case Npcstate.dead:
2024-11-29 15:13:32 +08:00
SetAni(0);
2024-10-22 17:37:42 +08:00
break;
}
2024-11-29 15:13:32 +08:00
}
//<2F>ܲ<EFBFBD><DCB2>߼<EFBFBD>
public void Run()
{
if (movebool)
{
SetAni(1);
transform.LookAt(target);
transform.position = Vector3.Lerp(transform.position, target.position, 0.3f * Time.deltaTime);
}
}
2024-10-22 17:37:42 +08:00
2024-11-29 15:13:32 +08:00
public void SetAni(int x)
{
anim.SetInteger("state", x);
2024-10-22 11:57:50 +08:00
}
2024-10-21 18:09:07 +08:00
2024-10-22 17:37:42 +08:00
2024-10-21 18:09:07 +08:00
}