using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum FlyProcess
{
None,
Up,
UpTurn,
Fly,
DownTurn,
Down
}
public class FlyControl : MonoBehaviour
{
private Coroutine corReady;
private float speedUpDown;
private float speedHorizontal;
private float speedTurn;
private float heightFixed;
private float error;
private float beforeDelay;
private FlyProcess process;
public FlyProcess Process
{
get { return process; }
set
{
process = value;
//((Main)main).FlyProcess = (int)process;
}
}
private string markId;
private bool isHover;
private float tempSpeed;
private Vector3 upPos = Vector3.zero;
private Quaternion upRot = Quaternion.identity;
private Vector3 downPos = Vector3.zero;
private Quaternion downRot = Quaternion.identity;
private Vector3 unitHor = Vector3.zero;
private Vector3 targetPos = Vector3.zero;
private float preDis;
private RaycastHit rayHit;
private bool isAltitude;
private void Start()
{
speedUpDown = 8 * Time.fixedDeltaTime;
speedHorizontal = 230 * 5 / 18 * Time.fixedDeltaTime;
speedTurn = 40;
heightFixed = 200;
error = 0.1f;//main.Properties.HGetDPFloat("误差范围(M)");
beforeDelay = 5;
isAltitude = true;
Process = FlyProcess.None;
}
private void FixedUpdate()
{
if (Process == FlyProcess.Up)
{
tempSpeed = Mathf.Lerp(tempSpeed, speedUpDown, 0.1f);
transform.position += new Vector3(0, tempSpeed, 0);
if (Mathf.Abs(upPos.y - transform.position.y) <= 0.1f)
{
tempSpeed = 0;
transform.position = upPos;
Process = FlyProcess.UpTurn;
}
}
else if (Process == FlyProcess.UpTurn)
{
//transform.rotation = Quaternion.Slerp(transform.rotation,upRot,speedTurn*Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, upRot, speedTurn * Time.deltaTime);
if (Quaternion.Angle(transform.rotation, upRot) < 1f)
{
transform.rotation = upRot;
preDis = Vector3.Distance(transform.position, downPos);
Process = FlyProcess.Fly;
}
}
else if (Process == FlyProcess.Fly)
{
tempSpeed += Time.deltaTime;
if (tempSpeed >= speedHorizontal)
tempSpeed = speedHorizontal;
transform.position += tempSpeed * unitHor;
//高度的问题
if (!isAltitude)
{
if (Physics.Raycast(transform.position, Vector3.down, out rayHit))
{
transform.position = new Vector3(transform.position.x, rayHit.point.y + heightFixed, transform.position.z);
}
}
if (Vector3.Distance(transform.position, downPos) <= error ||
Vector3.Distance(transform.position, downPos) >= preDis) //超出目标点一直飞的Bug
{
tempSpeed = 0;
transform.position = downPos;
Process = FlyProcess.DownTurn;
}
preDis = Vector3.Distance(transform.position, downPos);
}
else if (Process == FlyProcess.DownTurn)
{
//transform.rotation = Quaternion.Slerp(transform.rotation, downRot, speedTurn * Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, downRot, speedTurn * Time.deltaTime);
if (Quaternion.Angle(transform.rotation, downRot) < 1f)
{
transform.rotation = downRot;
Process = FlyProcess.Down;
}
}
else if (Process == FlyProcess.Down)
{
tempSpeed = speedUpDown;
tempSpeed = Mathf.Lerp(tempSpeed, 0, 0.5f);
transform.position -= new Vector3(0, tempSpeed, 0);
if (Mathf.Abs(targetPos.y - transform.position.y) <= 0.1f)
{
tempSpeed = 0;
transform.position = targetPos;
Process = FlyProcess.None;
//if (isHover)
//{
// CurStatus = (int)Status.FlyHover;
// ArriveMes();
//}
//else
//StartCoroutine(ToStopReady());
}
}
}
/// <summary>
///Step1: TakeOff 位置不变
///Step2: Flying 垂直起飞,高度? 调整转向
///Step3: 水平飞行
///Step4: Flying 垂直降落 调整转向
///Step5: A EngineOff B FlyingHover
/// </summary>
/// <param name="mark">目标标记点</param>
/// <param name="hover">是否悬停</param>
public void StartFlyProcess(GameObject mark, bool hover)
{
if (Process == FlyProcess.None)
{
//计算中间两个点的信息
upPos = transform.position + new Vector3(0, heightFixed, 0);
Vector3 updownPos = new Vector3(mark.transform.position.x, upPos.y, mark.transform.position.z);
downPos = mark.transform.position + new Vector3(0, heightFixed, 0);
upRot = Quaternion.LookRotation(updownPos - upPos);
unitHor = (downPos - upPos).normalized;
downRot = mark.transform.rotation;
targetPos = mark.transform.position;
////markId = mark.GetComponent<BObjectModel>().BObject.Id;
//isHover = hover;
////降落状态->开启
//if (main.CurStatus == (int)Status.Land)
//{
// corReady = StartCoroutine(ToFlyReady());
//}
////悬停状态->解锁
//if (main.CurStatus == (int)Status.FlyHover)
//{
// Process = FlyProcess.Up;
//}
}
if (Process == FlyProcess.Fly)
{
Vector3 updownPos = new Vector3(mark.transform.position.x, transform.position.y, mark.transform.position.z);
downPos = mark.transform.position + new Vector3(0, heightFixed, 0);
upRot = Quaternion.LookRotation(updownPos - upPos);
unitHor = (downPos - upPos).normalized;
downRot = mark.transform.rotation;
targetPos = mark.transform.position;
//markId = mark.GetComponent<BObjectModel>().BObject.Id;
isHover = hover;
Process = FlyProcess.UpTurn;
}
//}
//IEnumerator ToFlyReady()
//{
// yield return new WaitForSeconds(beforeDelay);
// main.CurStatus = (int)Status.TakeOff;
// yield return new WaitForSeconds(24f);
// main.CurStatus = (int)Status.Flying;
// Process = FlyProcess.Up;
//}
//IEnumerator ToStopReady()
//{
// main.CurStatus = (int)Status.EngineOff;
// yield return new WaitForSeconds(35f);
// main.CurStatus = (int)Status.Land;
// //ArriveMes();
//}
//private void ArriveMes()
//{
// sender.RunSend(SendType.MainToMain, markId, 203, main.BObjectId + ";" + ((Main)main).TargetId);
//}
}
}