1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public enum FlyProcess
    5. {
    6. None,
    7. Up,
    8. UpTurn,
    9. Fly,
    10. DownTurn,
    11. Down
    12. }
    13. public class FlyControl : MonoBehaviour
    14. {
    15. private Coroutine corReady;
    16. private float speedUpDown;
    17. private float speedHorizontal;
    18. private float speedTurn;
    19. private float heightFixed;
    20. private float error;
    21. private float beforeDelay;
    22. private FlyProcess process;
    23. public FlyProcess Process
    24. {
    25. get { return process; }
    26. set
    27. {
    28. process = value;
    29. //((Main)main).FlyProcess = (int)process;
    30. }
    31. }
    32. private string markId;
    33. private bool isHover;
    34. private float tempSpeed;
    35. private Vector3 upPos = Vector3.zero;
    36. private Quaternion upRot = Quaternion.identity;
    37. private Vector3 downPos = Vector3.zero;
    38. private Quaternion downRot = Quaternion.identity;
    39. private Vector3 unitHor = Vector3.zero;
    40. private Vector3 targetPos = Vector3.zero;
    41. private float preDis;
    42. private RaycastHit rayHit;
    43. private bool isAltitude;
    44. private void Start()
    45. {
    46. speedUpDown = 8 * Time.fixedDeltaTime;
    47. speedHorizontal = 230 * 5 / 18 * Time.fixedDeltaTime;
    48. speedTurn = 40;
    49. heightFixed = 200;
    50. error = 0.1f;//main.Properties.HGetDPFloat("误差范围(M)");
    51. beforeDelay = 5;
    52. isAltitude = true;
    53. Process = FlyProcess.None;
    54. }
    55. private void FixedUpdate()
    56. {
    57. if (Process == FlyProcess.Up)
    58. {
    59. tempSpeed = Mathf.Lerp(tempSpeed, speedUpDown, 0.1f);
    60. transform.position += new Vector3(0, tempSpeed, 0);
    61. if (Mathf.Abs(upPos.y - transform.position.y) <= 0.1f)
    62. {
    63. tempSpeed = 0;
    64. transform.position = upPos;
    65. Process = FlyProcess.UpTurn;
    66. }
    67. }
    68. else if (Process == FlyProcess.UpTurn)
    69. {
    70. //transform.rotation = Quaternion.Slerp(transform.rotation,upRot,speedTurn*Time.deltaTime);
    71. transform.rotation = Quaternion.RotateTowards(transform.rotation, upRot, speedTurn * Time.deltaTime);
    72. if (Quaternion.Angle(transform.rotation, upRot) < 1f)
    73. {
    74. transform.rotation = upRot;
    75. preDis = Vector3.Distance(transform.position, downPos);
    76. Process = FlyProcess.Fly;
    77. }
    78. }
    79. else if (Process == FlyProcess.Fly)
    80. {
    81. tempSpeed += Time.deltaTime;
    82. if (tempSpeed >= speedHorizontal)
    83. tempSpeed = speedHorizontal;
    84. transform.position += tempSpeed * unitHor;
    85. //高度的问题
    86. if (!isAltitude)
    87. {
    88. if (Physics.Raycast(transform.position, Vector3.down, out rayHit))
    89. {
    90. transform.position = new Vector3(transform.position.x, rayHit.point.y + heightFixed, transform.position.z);
    91. }
    92. }
    93. if (Vector3.Distance(transform.position, downPos) <= error ||
    94. Vector3.Distance(transform.position, downPos) >= preDis) //超出目标点一直飞的Bug
    95. {
    96. tempSpeed = 0;
    97. transform.position = downPos;
    98. Process = FlyProcess.DownTurn;
    99. }
    100. preDis = Vector3.Distance(transform.position, downPos);
    101. }
    102. else if (Process == FlyProcess.DownTurn)
    103. {
    104. //transform.rotation = Quaternion.Slerp(transform.rotation, downRot, speedTurn * Time.deltaTime);
    105. transform.rotation = Quaternion.RotateTowards(transform.rotation, downRot, speedTurn * Time.deltaTime);
    106. if (Quaternion.Angle(transform.rotation, downRot) < 1f)
    107. {
    108. transform.rotation = downRot;
    109. Process = FlyProcess.Down;
    110. }
    111. }
    112. else if (Process == FlyProcess.Down)
    113. {
    114. tempSpeed = speedUpDown;
    115. tempSpeed = Mathf.Lerp(tempSpeed, 0, 0.5f);
    116. transform.position -= new Vector3(0, tempSpeed, 0);
    117. if (Mathf.Abs(targetPos.y - transform.position.y) <= 0.1f)
    118. {
    119. tempSpeed = 0;
    120. transform.position = targetPos;
    121. Process = FlyProcess.None;
    122. //if (isHover)
    123. //{
    124. // CurStatus = (int)Status.FlyHover;
    125. // ArriveMes();
    126. //}
    127. //else
    128. //StartCoroutine(ToStopReady());
    129. }
    130. }
    131. }
    132. /// <summary>
    133. ///Step1: TakeOff 位置不变
    134. ///Step2: Flying 垂直起飞,高度? 调整转向
    135. ///Step3: 水平飞行
    136. ///Step4: Flying 垂直降落 调整转向
    137. ///Step5: A EngineOff B FlyingHover
    138. /// </summary>
    139. /// <param name="mark">目标标记点</param>
    140. /// <param name="hover">是否悬停</param>
    141. public void StartFlyProcess(GameObject mark, bool hover)
    142. {
    143. if (Process == FlyProcess.None)
    144. {
    145. //计算中间两个点的信息
    146. upPos = transform.position + new Vector3(0, heightFixed, 0);
    147. Vector3 updownPos = new Vector3(mark.transform.position.x, upPos.y, mark.transform.position.z);
    148. downPos = mark.transform.position + new Vector3(0, heightFixed, 0);
    149. upRot = Quaternion.LookRotation(updownPos - upPos);
    150. unitHor = (downPos - upPos).normalized;
    151. downRot = mark.transform.rotation;
    152. targetPos = mark.transform.position;
    153. ////markId = mark.GetComponent<BObjectModel>().BObject.Id;
    154. //isHover = hover;
    155. ////降落状态->开启
    156. //if (main.CurStatus == (int)Status.Land)
    157. //{
    158. // corReady = StartCoroutine(ToFlyReady());
    159. //}
    160. ////悬停状态->解锁
    161. //if (main.CurStatus == (int)Status.FlyHover)
    162. //{
    163. // Process = FlyProcess.Up;
    164. //}
    165. }
    166. if (Process == FlyProcess.Fly)
    167. {
    168. Vector3 updownPos = new Vector3(mark.transform.position.x, transform.position.y, mark.transform.position.z);
    169. downPos = mark.transform.position + new Vector3(0, heightFixed, 0);
    170. upRot = Quaternion.LookRotation(updownPos - upPos);
    171. unitHor = (downPos - upPos).normalized;
    172. downRot = mark.transform.rotation;
    173. targetPos = mark.transform.position;
    174. //markId = mark.GetComponent<BObjectModel>().BObject.Id;
    175. isHover = hover;
    176. Process = FlyProcess.UpTurn;
    177. }
    178. //}
    179. //IEnumerator ToFlyReady()
    180. //{
    181. // yield return new WaitForSeconds(beforeDelay);
    182. // main.CurStatus = (int)Status.TakeOff;
    183. // yield return new WaitForSeconds(24f);
    184. // main.CurStatus = (int)Status.Flying;
    185. // Process = FlyProcess.Up;
    186. //}
    187. //IEnumerator ToStopReady()
    188. //{
    189. // main.CurStatus = (int)Status.EngineOff;
    190. // yield return new WaitForSeconds(35f);
    191. // main.CurStatus = (int)Status.Land;
    192. // //ArriveMes();
    193. //}
    194. //private void ArriveMes()
    195. //{
    196. // sender.RunSend(SendType.MainToMain, markId, 203, main.BObjectId + ";" + ((Main)main).TargetId);
    197. //}
    198. }
    199. }