我试图让一架直升机从很远的地方起飞,然后降落在直升机停机坪上。我想让程序计算着陆距离和给定时间的速度。我想要的是直升机以恒定速度(飞行速度)从起始位置飞行到 flyToPos。然后我需要它着陆。这就是我真正陷入困境的地方。我需要直升机随着时间的推移慢慢降落。比如着陆时间是5秒,那么着陆需要5秒。我希望它随着时间的推移减慢到 minLandingSpeed。我搜索了类似的问题并尝试了一些公式并尝试了 Mathf.Lerp。那些也没有用。 我正在一个协程中编写我的所有代码,因为它的作用是有意义的。直升机一旦被召唤就必须做很多事情,比如预热旋翼、飞行、着陆、卸载和起飞。我尝试不使用协程,但效果不佳。下面是我使用的协程。
public IEnumerator DispatchToHelipadAndReturn(Vector3 LandingPosition, Vector3 lastLookAtPos)
{
unitStatus = UnitStatus.WarmingUp;
float takeOffRotorRate = (maxRotorSpeed / (RotorWarmUpTime));
//Warm up rotors
do
{
foreach (GameObject rotor in Rotors)
{
rotor.GetComponent<RotateContinuously>().RotationsPerMinute += takeOffRotorRate * Time.deltaTime;
}
yield return null;
} while (Rotors[Rotors.Count-1].GetComponent<RotateContinuously>().RotationsPerMinute<maxRotorSpeed);
//Set Take Off Trigger if there is one
if (TakeOffTrigger!="")
{
animator.SetTrigger(TakeOffTrigger);
}
TakeOffBegun?.Invoke(this, EventArgs.Empty);
//Calculate the flyToPos
float displace = ((FlightSpeed + MinLandingSpeed) / 2) *LandingTime;
float height = LandingPosition.y + displace;
Vector3 flyToPos = new Vector3(LandingPosition.x,height,LandingPosition.z);
float deccelRate = (Mathf.Pow(MinLandingSpeed, 2)-Mathf.Pow(FlightSpeed,2))/(2*displace);
//Move until it reaches target Pos
do
{
Debug.DrawLine(gameObject.transform.position, flyToPos, Color.black);
//Move to the fly to Pos
float step1 = FlightSpeed * Time.deltaTime;
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, flyToPos, step1);
Vector3 targetPosition = flyToPos - transform.position;
targetPosition.y = 0;
Quaternion targetRotation = Quaternion.LookRotation(targetPosition);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * RotationSpeed);
yield return null;
} while (Vector3.Distance(gameObject.transform.position,flyToPos)>0.1f);
float landingSpeed = FlightSpeed;
if (LandingTrigger!="")
{
animator.SetTrigger(LandingTrigger);
}
//Begin Landing
float t = 0;
float step = 0;
do
{
while (t<LandingTime)
{
t += Time.deltaTime;
float lerpValue = t / LandingTime;
step = Mathf.Lerp(FlightSpeed,MinLandingSpeed,lerpValue);
yield return null;
}
Debug.Log(step);
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, LandingPosition, step*Time.deltaTime);
Vector3 targetPos = lastLookAtPos - transform.position;
targetPos.y = 0;
Quaternion targetRotation = Quaternion.LookRotation(targetPos);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * RotationSpeed);
yield return null;
} while (Vector3.Distance(gameObject.transform.position,LandingPosition)>0.1f);
}
我正在使用 do while 循环来模仿某种意义上的更新功能。我有一种感觉,我的距离计算也可能是错误的。因此,我需要两件事。
感谢任何帮助。谢谢。