我编写了以下方法,用于将任意点绕另一个任意点旋转一定角度,并在一段时间内完成旋转。该点现在运动不规律,但最终到达了我认为是期望位置的附近。我需要让它平滑地移动到目标角度。
请注意,这些点与游戏对象无关。
从下面的图中可以看出,我试图将贝塞尔曲线的一个点(使用
请注意,这些点与游戏对象无关。
从下面的图中可以看出,我试图将贝塞尔曲线的一个点(使用
LineRenderer
绘制)绕另一个点旋转一定角度,并在一定时间内完成旋转。这些点中没有一个与包含贝塞尔曲线的游戏对象的位置重合。
IEnumerator runMovement() {
yield return new WaitForSeconds(2.0f);
Vector2 pivot = points[3];
StartCoroutine(RotateControlPointWithDuration(pivot, 2.0f, 90.0f));
}
IEnumerator RotateControlPointWithDuration(Vector2 pivot, float duration, float angle)
{
float currentTime = 0.0f;
float ourTimeDelta = 0;
Vector2 startPos = points[0];
ourTimeDelta = Time.deltaTime;
float angleDelta = angle / duration; //how many degress to rotate in one second
while (currentTime < duration)
{
currentTime += Time.deltaTime;
ourTimeDelta = Time.deltaTime;
points[0] = new Vector2(Mathf.Cos(angleDelta * ourTimeDelta) * (startPos.x - pivot.x) - Mathf.Sin(angleDelta * ourTimeDelta) * (startPos.y - pivot.y) + pivot.x,
Mathf.Sin(angleDelta * ourTimeDelta) * (startPos.x - pivot.x) + Mathf.Cos(angleDelta * ourTimeDelta) * (startPos.y - pivot.y) + pivot.y);
yield return null;
}
}