我需要GameObject通過 z 軸將 2D 旋轉 90 度。所以我已經嘗試過transform.RotateAround了,但仍然旋轉了一個完整的圓圈:
transform.RotateAround(target.transform.position, new Vector3 (0, 0, -90),
50f * Time.deltaTime)
我需要在 90 之前停止它。我該怎么做?
在此處輸入影像描述
uj5u.com熱心網友回復:
創建一個基于旋轉的協程deltaTime并跟蹤它旋轉了多遠,一旦碰到就停止90。使用Mathf.Min以確保不要旋轉超過 90。
private isRotating = false;
// speed & amount must be positive, change axis to control direction
IEnumerator DoRotation(float speed, float amount, Vector3 axis)
{
isRotating = true;
float rot = 0f;
while (rot < amount)
{
yield return null;
float delta = Mathf.Min(speed * Time.deltaTime, amount - rot);
transform.RotateAround(target.transform.position, axis, delta);
rot = delta;
}
isRotating = false;
}
然后,當您想開始旋轉時,如果協程尚未旋轉,請啟動協程:
if (!isRotating)
{
StartCoroutine(DoRotation(50f, 90f, Vector3.back));
}
保存協程本身而不是使用標志稍微好一點。這可以讓你做一些事情,比如在任何時候停止旋轉。
private Coroutine rotatingCoro;
// speed & amount must be positive, change axis to control direction
IEnumerator DoRotation(float speed, float amount, Vector3 axis)
{
float rot = 0f;
while (rot < amount)
{
yield return null;
float delta = Mathf.Min(speed * Time.deltaTime, amount - rot);
transform.RotateAround(target.transform.position, axis, delta);
rot = delta;
}
rotatingCoro = null;
}
// ...
if (rotatingCoro != null)
{
rotatingCoro = StartCoroutine(DoRotation(50f, 90f, Vector3.back));
}
// ...
// call to stop rotating immediately
void StopRotating()
{
if (rotatingCoro != null)
{
StopCoroutine(rotatingCoro);
rotatingCoro = null;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/449571.html
上一篇:將透視腳本更改為正交統一2d
下一篇:不加入房間||光子雙關語
