问题描述
我编写了以下方法,可以在持续时间内围绕任意点旋转任意点一个角度.现在,所讨论的问题不固定,但最终绕到了我认为是理想目的地的地方.我需要使它平稳地移动到一定角度.
I have written the following methods to rotate an arbitrary point around another arbitrary point by an angle over a duration. The point in question now moves erratically, but ends up around where I believe is the desired destination. I need to get it to move smoothly to the angle.
请注意,这些点与游戏对象无关.
从下图开始,我试图在给定的时间段内将贝塞尔曲线的一个点(用LineRenderer
绘制)绕另一点移动一个角度.这些点都不与包含贝塞尔曲线的游戏对象的位置重合.
From the diagram below, I am trying to move one point of a bezier curve (drawn with a LineRenderer
) around another point by an angle over a given period of time. None of these points coincide with the position of the game object which contains the bezier curve.
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;
}
}
推荐答案
您想要的模式只是
public IEnumerator HowToSmoothly()
{
// move "X" from value "A" to value "B"
float duration = 2.5f;
float delta = B - A;
float startTime = Time.time;
float finishTime = Time.time+duration;
while(Time.time<finishTime)
{
float soFarTime = Time.time-startTime;
float fractionThisFrame = soFarTime / duration;
float valueThisFrame = A + delta * fractionThisFrame;
X = valueThisFrame
if (X > B) X = B;
yield return 0;
}
X = B;
yield break;
}
这篇关于围绕另一个任意点旋转任意点的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!