FishManage.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. public class FishManage : MonoBehaviour
  6. {
  7. //// Start is called before the first frame update
  8. //public Transform fishschoolObj, ParticleSystemobj;
  9. //public float deleTime1, deleScaleTime1;
  10. //public Vector3 ScalePos1;
  11. //public float deleTime2, deleScaleTime2;
  12. //public Vector3 ScalePos2;
  13. //public float deleTime3, deleScaleTime3;
  14. //public Vector3 ScalePos3;
  15. //public float PlayerdeleScaleTime;
  16. //public Vector3 PlayerScaleMaxPos;
  17. //public Vector3 PlayerScaleMiniPos;
  18. //public float ToPlayerSpeed;
  19. //private float distacneToPlayer;
  20. //private Vector3 Player;
  21. public Transform Group;
  22. public TankBehaviour []Finsh;//鱼儿
  23. public Transform[] FishPos;//人群靠近后逃跑鱼群游动自动AI位置
  24. private int _isFunction = 1;//当为1的时候,启用鱼群自动导航状态,
  25. public Transform startPosition;//Group初始位置
  26. private float distacneToPlayer;//人物距离位置
  27. public GameObject player;//玩家
  28. private bool _IsWhetherToPlayer = true;//开始走向人物
  29. public bool _isVuforia;//判断为SvrCamera还是vuforiaCamera;
  30. void Start()
  31. {
  32. //if (!_isVuforia)
  33. //{
  34. // player = SvrManager.Instance.head.gameObject;
  35. //}
  36. StartCoroutine(RandomFunction());
  37. //this.gameObject.GetComponent<SCButton>().onClick.AddListener(() =>
  38. //{
  39. //});
  40. }
  41. ////// Update is called once per frame
  42. private int getRangeNum = 0; //最后输出的与上次随机数不同的随机数
  43. public int GetRangeNum
  44. {
  45. get => getRangeNum;
  46. set
  47. {
  48. getRangeNum = value;
  49. if (getRangeNum == 0)
  50. {
  51. // Group.transform.position = FishPos[0].position;\
  52. Group.DOMove(FishPos[0].transform.position, 1f, false);//deleTime为规定时间内移动到位置
  53. }
  54. else if (getRangeNum == 1)
  55. {
  56. // Group.transform.position = FishPos[1].position;
  57. Group.DOMove(FishPos[1].transform.position, 1f, false);//deleTime为规定时间内移动到位置
  58. }
  59. else if (getRangeNum == 2)
  60. {
  61. //Group.transform.position = FishPos[2].position;
  62. Group.DOMove(FishPos[2].transform.position, 1f, false);//deleTime为规定时间内移动到位置
  63. }
  64. if (_IsWhetherToPlayer==true)
  65. {
  66. StartCoroutine(FinshPos1(20f)) ;
  67. _IsWhetherToPlayer = false;
  68. }
  69. }
  70. }
  71. int rangeRadomNum = 0; //每次随机产生的随机数,用于与上次进行比较
  72. int numCount = 0; //用于统计每次输出的数,方便观察
  73. int oneType=0;
  74. IEnumerator RandomFunction()
  75. {
  76. while (_isFunction == 1)
  77. {
  78. if (oneType == 0)
  79. {
  80. yield return new WaitForSeconds(0.1f);
  81. StartCoroutine(SpeedUpFinshMessage(0.1f, 30, 100));
  82. StartCoroutine(SpeedDownFinshMessage(2f, 9, 40));
  83. }
  84. else
  85. {
  86. yield return new WaitForSeconds(7f);
  87. }
  88. numCount++;
  89. do
  90. {
  91. rangeRadomNum = Random.Range(0, 2);
  92. }
  93. while (GetRangeNum == rangeRadomNum);
  94. GetRangeNum = rangeRadomNum;
  95. oneType++;
  96. // Debug.Log("第" + numCount.ToString() + "得到的随机数:" + GetRangeNum);
  97. }
  98. while (_isFunction==2)
  99. {
  100. yield return new WaitForSeconds(1f);
  101. distacneToPlayer = Vector3.Distance(Group.transform.position, player.transform.position);
  102. // Debug.Log("第数:" + distacneToPlayer);
  103. if (distacneToPlayer <= 1.2)
  104. {
  105. _IsWhetherToPlayer = true;
  106. _isFunction = 1;
  107. StartCoroutine(RandomFunction());
  108. }
  109. else
  110. {
  111. Group.transform.position = startPosition.position;
  112. }
  113. }
  114. }
  115. public IEnumerator FinshPos1(float PosNum)
  116. {
  117. yield return new WaitForSeconds(PosNum);
  118. StopAllCoroutines();
  119. _isFunction = 2;
  120. StartCoroutine(RandomFunction());
  121. oneType = 0;
  122. StartCoroutine(SpeedUpFinshMessage(0.1f, 13, 60)) ;
  123. StartCoroutine(SpeedDownFinshMessage(2f, 9, 40)) ;
  124. // Group.DOMove(FishPos[PosNum].transform.position, deleTime, false);//deleTime为规定时间内移动到位置
  125. }
  126. /// <summary>
  127. /// 加速
  128. /// </summary>
  129. /// <param name="deletime"></param>
  130. /// <param name="Speed"></param>
  131. /// <param name="RotateSpeed"></param>
  132. /// <returns></returns>
  133. public IEnumerator SpeedUpFinshMessage(float deletime ,float Speed, float RotateSpeed )
  134. {
  135. yield return new WaitForSeconds(deletime);
  136. for (int i = 0,count= Finsh.Length; i < count; i++)
  137. {
  138. Finsh[i].moveSpeed = Speed+i;
  139. Finsh[i].rotateSpeed = RotateSpeed+i;
  140. }
  141. }
  142. /// <summary>
  143. /// 减速
  144. /// </summary>
  145. /// <param name="deletime"></param>
  146. /// <param name="Speed"></param>
  147. /// <param name="RotateSpeed"></param>
  148. /// <returns></returns>
  149. public IEnumerator SpeedDownFinshMessage(float deletime, float Speed, float RotateSpeed)
  150. {
  151. yield return new WaitForSeconds(deletime);
  152. for (int i = 0, count = Finsh.Length; i < count; i++)
  153. {
  154. Finsh[i].moveSpeed = Speed + i;
  155. Finsh[i].rotateSpeed = RotateSpeed + i;
  156. }
  157. }
  158. ///// <summary>
  159. ///// 放大鱼群
  160. ///// </summary>
  161. ///// <param name="intervalTime"></param>
  162. ///// <param name="ScaleSize"></param>
  163. ///// <param name="deleScaleTime"></param>
  164. ///// <returns></returns>
  165. //public IEnumerator FishMoveToPlayer( float intervalTime, Vector3 ScaleSize, float deleScaleTime)
  166. //{
  167. // yield return new WaitForSeconds(intervalTime);
  168. // ParticleSystemobj.DOScale(ScaleSize, deleScaleTime); //deleScaleTime为规定时间内放大
  169. //}
  170. ///// <summary>
  171. ///// 到达人物位置
  172. ///// </summary>
  173. ///// <returns></returns>
  174. //public IEnumerator ArrivePlayerPos(float intervalTime, Vector3 ScaleSize, float deleScaleTime)
  175. //{
  176. // yield return new WaitForSeconds(intervalTime);
  177. // ParticleSystemobj.DOScale(ScaleSize, deleScaleTime); //deleScaleTime为规定时间内放大
  178. // // Debug.Log("缩小鱼群");
  179. // yield return new WaitForSeconds(5);
  180. // _isFunction = 1;
  181. // StartCoroutine(RandomFunction());
  182. //}
  183. }