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.
Please note that the points are independent of a game object.
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;
}
}