围绕另一个任意点旋转任意点的问题

Ten*_*Ten 5 c# unity-game-engine

我已经编写了以下方法来在一段持续时间内以任意角度围绕另一个任意点旋转任意点.这个问题现在变得不规律,但最终我认为是理想的目的地.我需要让它顺利地移动到角度.

请注意,这些点与游戏对象无关.

从下图中,我试图LineRenderer在给定的时间段内将一个贝塞尔曲线的一个点(用a绘制)绕另一个点移动一个角度.这些点中没有一个与包含贝塞尔曲线的游戏对象的位置一致. 


在此输入图像描述

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

pse*_*dul 0

我确实可以在 quartonians 方面为您提供很多帮助,但我可以提供一些有关随时间推移的移动的建议。我必须吸取的惨痛教训是尽可能避免使用协同例程。虽然它们看起来很漂亮,但正确使用它们有点令人困惑,而且 Unity 使用它们的方式并不完全是您在 C# 的其他领域中应该使用它们的方式。

只要有可能,我就使用更新每个帧的统一类。它更容易理解和调试。您的情况的缺点是您尝试旋转的每个点都需要一个新组件,但这应该不是什么大问题。

public class PointRotator : MonoBehaviour
Run Code Online (Sandbox Code Playgroud)

{

bool rotating = false;
float rate = 0;
float angle;

Vector3 point;
Vector3 pivot;

public void Rotate(Vector3 point, Vector3 pivot, float duration, float angle)
{
    this.point = point;
    this.pivot = pivot;
    this.rate = angle/duration;
    this.angle = angle;

    rotating = true;
}

public void Update()
{
    if (rotating)
    {
        // use quartonian.Lerp with Time.deltatime here

    //if(angle > quartonian angle){rotating = false)
    }
}
Run Code Online (Sandbox Code Playgroud)

}

尝试类似的方法,看看您的问题是否消失。否则,您可能需要更多地研究四方量,或者因为您处于 2D 平面中而手动进行三角函数。