[英]Rotate GameObject around pivot point over time
I am rotating a point in relation to another pivot point using the following code: 我使用以下代码旋转与另一个轴点相关的点:
Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) {
Vector3 dir = point - pivot; // get point direction relative to pivot
dir = Quaternion.Euler(angles) * dir; // rotate it
point = dir + pivot; // calculate rotated point
return point;
}
How can I rotate the point over a specific duration, say 5 seconds using a coroutine? 如何在一个特定的持续时间内旋转该点,比如使用一个协程5秒?
Modified the code from my other answer to get this effect. 修改了我的其他答案中的代码以获得此效果。 You can read that to understand how lerp work.
您可以阅读它以了解lerp的工作原理。
What changed and why: 改变了什么,为什么:
1 .Got the beginRotation
point from the RotatePointAroundPivot
function by passing Vector3.zero
to the angle parameter and the current Object position outside of the while
loop. 1.通过将
Vector3.zero
传递给angle参数和while
循环之外的当前Object位置,从RotatePointAroundPivot
函数beginRotation
点。 This needs to be done once as the result will be used in the while
loop. 这需要完成一次,因为结果将在
while
循环中使用。
Vector3 beginRotPoint = RotatePointAroundPivot(objPoint.transform.position, pivot, Vector3.zero);
This is done because lerp needs a starting point. 这样做是因为lerp需要一个起点。 That starting point should never changed otherwise, it break the lerp function.
那个起点应该永远不会改变,它打破了lerp功能。
2 .Generate new angle each frame in the while
loop. 2.在
while
循环中生成每个帧的新角度。 This is done by lerping from Vector3.zero
to the target angle: 这是通过从
Vector3.zero
到目标角度进行的:
float t = counter / duration;
Vector3 tempAngle = Vector3.Lerp(Vector3.zero, angles, t);
3 .Find the final pivot angle rotation. 3。找到最终的转角旋转。 This is done by passing the starting point from #1 to the first parameter, the pivot point to the second parameter and finally the current angle generated from #2 to the third parameter.
这是通过将起点从#1传递到第一个参数,枢轴点到第二个参数,最后是从#2到第三个参数生成的当前角度来完成的。
Vector3 tempPivot = RotatePointAroundPivot(beginRotPoint, pivot, tempAngle);
4 .Finally, assign the result from #3 to objPoint.transform.position
instead of objPoint.transform.eulerAngles
because you are not only moving the object. 4。最后,将#3的结果分配给
objPoint.transform.position
而不是objPoint.transform.eulerAngles
因为您不仅要移动对象。 You are also rotating it. 你也在旋转它。
objPoint.transform.position = tempPivot;
Complete code: 完整代码:
Your RotatePointAroundPivot
function: 你的
RotatePointAroundPivot
函数:
Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
{
Vector3 dir = point - pivot; // get point direction relative to pivot
dir = Quaternion.Euler(angles) * dir; // rotate it
point = dir + pivot; // calculate rotated point
return point;
}
Modified rotateObject
function that I described what changed above: 修改了
rotateObject
函数,我描述了上面改变了什么:
bool rotating = false;
IEnumerator rotateObject(GameObject objPoint, Vector3 pivot, Vector3 angles, float duration)
{
if (rotating)
{
yield break;
}
rotating = true;
Vector3 beginRotPoint = RotatePointAroundPivot(objPoint.transform.position, pivot, Vector3.zero);
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
float t = counter / duration;
Vector3 tempAngle = Vector3.Lerp(Vector3.zero, angles, t);
Vector3 tempPivot = RotatePointAroundPivot(beginRotPoint, pivot, tempAngle);
objPoint.transform.position = tempPivot;
Debug.Log("Running: " + t);
yield return null;
}
rotating = false;
}
USAGE : 用法 :
Will rotate object 180-deg around pivot point in 5 seconds: 将在5秒内绕枢轴点旋转物体180度:
//The GameObject with the pivot point to move
public GameObject pointToRotate;
//The Pivot point to move the GameObject around
public Transform pivotPoint;
//The angle to Move the pivot point
public Vector3 angle = new Vector3(0f, 180f, 0f);
//The duration for the rotation to occur
public float rotDuration = 5f;
void Start()
{
StartCoroutine(rotateObject(pointToRotate, pivotPoint.position, angle, rotDuration));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.