NPC_QW.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using DG.Tweening;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using UnityEngine;
  6. public class NPC_QW : MonoBehaviour
  7. {
  8. // Start is called before the first frame update
  9. private float distacneToPlayer;
  10. public GameObject player;
  11. public Transform[] goToPoint;
  12. public Animator anim;
  13. private Transform _targetPoint;
  14. private int _index = 0;
  15. private bool _IsRun = true;
  16. public Vector2 turnAnddeletTime;//x为转弯速度,y为规定时间到达。
  17. public Vector2 turnAnddeletTime1;//x为转弯速度,y为规定时间到达。
  18. public Vector2 turnAnddeletTime2;//x为转弯速度,y为规定时间到达。
  19. public Vector2 turnAnddeletTime3;//x为转弯速度,y为规定时间到达。
  20. public bool _isVuforia;//判断为SvrCamera还是vuforiaCamera;
  21. public int index
  22. {
  23. get => _index++;
  24. set
  25. {
  26. }
  27. }
  28. private void OnEnable()
  29. {
  30. //if (!_isVuforia)
  31. //{
  32. // player = SvrManager.Instance.head.gameObject;
  33. //}
  34. StartCoroutine(CrabAI());
  35. }
  36. void Start()
  37. {
  38. }
  39. //private void Update()
  40. //{
  41. // //_targetPoint = v3Point[index];
  42. // // this.transform.DOLocalPath(v3Point[], 10f, PathType.CatmullRom);
  43. // // this.transform.DOLookAt(goToPoint[0].position, 0.5f);
  44. // // this.transform.DOMove(goToPoint[0].transform.position, 10f, false);
  45. // //transform.LookAt(v3Point[index]);
  46. // //transform.DOBlendableLocalMoveBy(Vector3.forward * 10, 3);
  47. // if (Input.GetKeyUp(KeyCode.A))
  48. // {
  49. // CrabPath(turnAnddeletTime.x, turnAnddeletTime.y);
  50. // }
  51. // if (Input.GetKeyUp(KeyCode.B))
  52. // {
  53. // CrabPath(turnAnddeletTime.x, turnAnddeletTime.y);
  54. // }
  55. // if (Input.GetKeyUp(KeyCode.C))
  56. // {
  57. // CrabPath(turnAnddeletTime.x, turnAnddeletTime.y);
  58. // }
  59. // if (Input.GetKeyUp(KeyCode.D))
  60. // {
  61. // CrabPath(turnAnddeletTime.x, turnAnddeletTime.y);
  62. // }
  63. //}
  64. public IEnumerator CrabAI()
  65. {
  66. while (true)
  67. {
  68. yield return new WaitForSeconds(1f);
  69. if(player!=null)
  70. {
  71. distacneToPlayer = Vector3.Distance(this.transform.position, player.transform.position);
  72. // Debug.Log(string.Format("{0}====={1}", transform.name, distacneToPlayer));
  73. if (distacneToPlayer < 1.8f && _IsRun == true)
  74. {
  75. // anim.SetBool("IsRun", true);
  76. // anim.SetBool("isWait", false);
  77. // this.transform.DOMove(goToPoint.transform.position, 10f, false);
  78. if (_index == 0)
  79. {
  80. // CrabPath(1f,5f);
  81. CrabPath(turnAnddeletTime.x, turnAnddeletTime.y);
  82. }
  83. else if (_index == 1)
  84. {
  85. // CrabPath(1f,5f);
  86. CrabPath(turnAnddeletTime.x, turnAnddeletTime.y);
  87. }
  88. else if (_index == 2)
  89. {
  90. // CrabPath(1f,5f);
  91. CrabPath(turnAnddeletTime.x, turnAnddeletTime.y);
  92. }
  93. else if (_index == 3)
  94. {
  95. // CrabPath(1f, 5f);
  96. CrabPath(turnAnddeletTime.x, turnAnddeletTime.y);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. public void CrabPath(float turnTime, float deletTime)
  103. {
  104. _IsRun = false;
  105. anim.SetBool("IsRun", true);
  106. this.transform.DOLookAt(goToPoint[_index].position, turnTime);
  107. this.transform.DOMove(goToPoint[_index].transform.position, deletTime, false);
  108. // Debug.Log("Run");
  109. StartCoroutine(Stoppath(turnTime, deletTime));
  110. }
  111. public IEnumerator Stoppath(float turnTime, float deletTime)
  112. {
  113. float Time = deletTime - 0.35f;
  114. yield return new WaitForSeconds(Time);
  115. // this.transform.DOLookAt(goToPoint[_index].position, turnTime);
  116. // Debug.Log("stop");
  117. anim.SetBool("IsRun", false);
  118. _index++;
  119. _IsRun = true;
  120. if (_index >= goToPoint.Length)
  121. {
  122. _index = 0;
  123. }
  124. }
  125. }